我是java的初学者,我正在尝试逐个字符比较java中的两个字符串,并通过以下代码找到它们有多少不同的字符,但它不起作用,
I am beginner in java, I am trying to compare two strings in java char by char and find how many different chars they have by the following code but it doesn't work,
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++;
}
}`
有什么建议吗?
使用这个:
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++;
}
}
这篇关于逐个字符比较java中的两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)