我需要计算两个字符串中不匹配字符的数量。例如
string 1 "hari", string 2 "malar"
现在我需要从两个字符串中删除重复项 ['a' & 'r'] 在两个字符串中都很常见,所以删除它,现在 string 1 包含“hi” string 2 包含“mla”.
剩余计数 = 5
我试过这段代码,如果重复/重复在同一个字符串中不可用,它工作正常,就像这里 'a' 在 string 2 中出现两次,所以我的代码不能正常工作。
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < second.length; j++) {
if(first[i] == second[j])
{
getstrings = new ArrayList<String>();
count=count+1;
Log.d("Matches", "string char that matched "+ first[i] +"==" + second[j]);
}
}
}
int tot=(first.length + second.length) - count;
这里的first & second指的是
char[] first = nameone.toCharArray();
char[] second = nametwo.toCharArray();
此代码适用于 String 1 "sri"string 2 "hari"
这里的字符串字符没有重复,所以上面的代码可以正常工作。帮我解决这个问题?
最佳答案
这是我的解决方案,
public static void RemoveMatchedCharsInnStrings(String first,String second)
{
for(int i = 0 ;i < first.length() ; i ++)
{
char c = first.charAt(i);
if(second.indexOf(c)!= -1)
{
first = first.replaceAll(""+c, "");
second = second.replaceAll(""+c, "");
}
}
System.out.println(first);
System.out.println(second);
System.out.println(first.length() + second.length());
}
希望这是您所需要的。如果没有,我会更新我的答案
关于java - 如何获取两个字符串中不匹配字符的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949977/