how to compare two strings in java?(如何比较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中的两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何比较java中的两个字符串?


- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01