如何比较java中的两个字符串?
我有如下情况列表:字符串 A <-------> 字符串 B
我应该在以下条件下比较 A 和 B:
1-如果read中显示的数字两边不同,但其余数字相同,则表示A = B.
1- if number shown in read is different in both side but the rest is the same it means A = B.
2- 在某些情况下,例如 A 侧的第一个在红色显示的数字之后没有空格,但在 b 侧,在红色显示的数字和 X 之后有一个空格.
2- there is in some situation like the first one in A side after number shown in red there is not white space but in b side there is a white space after number shown in red and also after X.
3- 也有不同的情况,比如数字 3
3- there is also different cases like number 3
现在我怎样才能最好地比较这两个字符串?
private static void controlSimilarity(String memo,String ck,String bc,String id,String product) {
if(!id.equals(product)){
listIdentifier.add(new MmoCnBcIdProduct(memo,ck,bc,id,product));
}
推荐答案
我会尝试规范化"字符串.将两者都设为大写,将s+Xs+"替换为X",将s+%"替换为%",将s+MG"替换为MG"等,然后用空格或一些正则表达式将其分割(Scanner 类或 Guava 的 Splitter)并比较字符串的各个部分.
I'd try to "normalize" strings. Make both uppercase, replace "s+Xs+" with "X", replace "s+%" with "%", "s+MG" with "MG" etc., then split it by whitespaces or some regex (Scanner class or Guava's Splitter) and compare parts of the string.
相关文章