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

查看:173
本文介绍了如何在Java中比较两个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下情况列表:字符串A< ------->字符串B

我应该在以下条件下将A与B进行比较:

1-如果读取的数字在两侧均不同,但其余部分相同,则表示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 + X \ s +"替换为"X",将"\ s +%"替换为%",将"\ s + MG"替换为"MG"等,然后按空格或某些正则表达式进行拆分(扫描程序类或Guava的Splitter),然后比较字符串的各个部分.

I'd try to "normalize" strings. Make both uppercase, replace "\s+X\s+" 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天全站免登陆