in C + +, the code for a two string comparison can be:
(string1==string2)
But in Java, this code returns false even if two strings are exactly the same.
string1.equals (string2) must be used in Java to determine
Supplement
if:
string s1= "Hello";
string s2= "Hello";
then (S1==S2) =true;
because they point to the same object.
if:
string S1=new string ("Hello");
string S2=new string ("Hello");
then (S1==S2) =false
if the values of the other variables are assigned to S1 and S2, even if the contents are the same, false is returned because they do not point to the same object. so it is recommended to use Equals (), because equals is the real content compared to the
For example:
string String1=new string ("AAA");
string String2=new string ("AAA");
the two strings should of course be equal.
if string1==string2 with an expression, the value of the expression is False
true if the expression string1.equals (string2) is the value of the expression
therefore should be used String1.equals (string2), in the IF statement is
if (string1.equals (string2) ==true)//String equal, ...
String1==string2, the values are equal, and the memory addresses are equal, exactly equal
string1.equals (string2) is true, only values are equal
If you compare the size of a string using: Str1.compareto (string str2)
compares two strings in a dictionary order. The comparison is based on the individual characters in the stringUnicode Value. PressDictionary ordercompares the sequence of characters represented by this string object with the sequence of characters represented by the argument string. If this string object is in the dictionary order in the parameter stringbefore, the comparison result is aNegative integer. If this string object is in the dictionary order after the parameter string, the comparison result is a positive integer. If these two stringsequal, the result is 0;CompareTo returns 0 only if the method equals (Object) returns True.
This is the definition of the dictionary sort. If the two strings are different, they either have different characters at an index (both are valid indexes), are either of different lengths, or both. If they have different characters at one or more index positions, suppose K is the minimum value for such an index, then the string with the smaller value on position K (using the < operator), whose dictionary order precedes the other string. In this case, COMPARETO returns the difference between the two strings at position K at two char values, which is the value:
This.charat (k)-anotherstring.charat (k)
If there are no different index positions for the characters, the dictionary order of shorter strings precedes the longer strings. In this case, COMPARETO returns the difference between the two string lengths, that is, the value:
This.length ()-anotherstring.length ()
Comparison of string equality and size in Java