Comparing strings in java

前端 未结 7 1497
别那么骄傲
别那么骄傲 2020-12-22 05:58
String string1 = \"Hi there\";
String string2 = \"Hi\";
String string3 = \"Hi\";

System.out.println(string1.substring(0, string2.length()) == string2); //1
System.o         


        
7条回答
  •  孤城傲影
    2020-12-22 06:45

    The == operator is comparing 2 references to the same string for equality.

    The String class has two methods that you should use to compare two strings for equality:

    String1.equals(String2);
    

    Returns true if String1 and String2 are identical (including the case of the letters).

    If you don't care about the case then you can use:

    String1.equalsIgnoreCase(String2);
    

    This returns true if String1 and String2 are equal regardless of the case (obviously).

提交回复
热议问题