逐字符比较java中的两个字符串

mik*_*ike 8 java string char compareto

我是java的初学者,我试图通过char比较java char中的两个字符串,并通过以下代码查找它们有多少不同的字符,但它不起作用,

     min is the min between the 2 strings

     for(int i=0; i<min-1; i++){
            s1 = w1.substring(j,j++);
            s2 = w2.substring(j,j++);

            if (! s1.equalsIgnoreCase(s2) ){
                counter++;    
            }
      }`
Run Code Online (Sandbox Code Playgroud)

有小费吗?

Baz*_*Baz 10

用这个:

char[] first  = w1.toLowerCase().toCharArray();
char[] second = w2.toLowerCase().toCharArray();

int minLength = Math.min(first.length, second.length);

for(int i = 0; i < minLength; i++)
{
        if (first[i] != second[i])
        {
            counter++;    
        }
}
Run Code Online (Sandbox Code Playgroud)


Raz*_*van 5

使用charAt(index)方法并对两个字符使用'=='运算符:

c1 = w1.charAt(j);
c2 = w2.charAt(j);

if (c1 == c2) ){
   counter++;    
}
Run Code Online (Sandbox Code Playgroud)