Java String Difference difference(String str1, String str2)

Here you can find the source of difference(String str1, String str2)

Description

Returns the number of characters in the two Strings that are the same.

License

Open Source License

Parameter

Parameter Description
str1 a String.
str2 a String.

Return

The number of characters in the two Strings that are the same.

Declaration

public static int difference(String str1, String str2) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

public class Main {
    /**/*from   w  w  w . j a v  a  2  s  .c  o  m*/
     * Returns the number of characters in the two Strings that are the same.
     * 
     * @param str1 a String.
     * @param str2 a String.
     * @return The number of characters in the two Strings that are the same.
     * 
     */
    public static int difference(String str1, String str2) {
        if (str1 == null || str2 == null) {
            return 0;
        }
        int lengthToMatch = Math.min(str1.length(), str2.length());
        int diff = 0;
        for (int i = 0; i < lengthToMatch; i++) {
            if (str1.charAt(i) == str2.charAt(i)) {
                diff++;
            }
        }
        return diff;
    }
}

Related

  1. difference(String s1, String s2)
  2. difference(String str1, String str2)
  3. difference(String str1, String str2)
  4. difference(String str1, String str2)
  5. difference(String str1, String str2)