如何比较java中的两个字符串? [英] how to compare two strings in java?

查看:25
本文介绍了如何比较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.

这篇关于如何比较java中的两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆