比较java中的2个字符串和特殊字符

时间:2015-04-02 08:24:33

标签: java

如何将2个字符串与特殊字符进行比较? 我有如下字符串,我可以知道如何比较它们吗?

strA = "AC-11234X-DD+++1"
strB = "AC-11234X-DD+++1"

我测试了matches()equals()equalsIgnoreCase()都无效。

if (strA.matches(strB)){
...
} else {
..
}

4 个答案:

答案 0 :(得分:-1)

此代码检查这两个字符串是否相等

String strA = "AC-11234X-DD+++1" ;
String strB = "AC-11234X-DD+++1";    

if(strA.equals(strB))
       //they are equal
else
       //they are not

答案 1 :(得分:-1)

为什么不尝试

的System.out.println(strA.hashCode()== strB.hashCode());

如果matches(),equals(),equalsIgnoreCase()不起作用。

如果您对此结果不满意,可以尝试覆盖compareTo方法并拥有自己的逻辑。

答案 2 :(得分:-1)

public static void main(String[] args)
{
   String strA = "AC-11234X-DD+++1";
    String strB = "AC-11234X-DD+++1";

    System.out.println(strA.equals(strB));
    }

这很有效。

答案 3 :(得分:-1)

必须使用 compareTo 方法。

此方法返回的值是 int

  • 如果是> 0表示第二个字符串先于字母顺序排在第一个字符串
  • 如果是= 0则表示两个字符串相等;
  • 如果是< 0表示fisrt字符串按字母顺序排在第二个字符串之前

关于您的问题的一个例子(非常粗略)可能是这样的:

int r = A.compareTo(B);

if(r > 0) { 
  System.out.println("B comes before A in alphabetical order");
} else if(r < 0) {
  System.out.println("A comes before B string in alphabetical order");
} else {
  System.out.println("The two strings are equal");
}
相关问题