You can get the length of a string using the length() method in the String class, for example:
Stringchina=Victory of the Anti-Japanese War in 1945;intn1,n2;n1=china.length();n2=dotcpp.length();
Then, the value of n1 is 9 and the value of n2 is 5 at this time.
The string object calls the equals(String s) method to compare whether the entity of the current string object is the same as the entity of the string specified by parameter s, for example:
Stringtom=newString(God rewards those who work hard);Stringboy=newString(Humanity rewards those who work hard);Stringjerry=newString(God rewards those who work hard);
Then, at this time, the value of tom.equals(boy) is false, and the value of tom.equals(jerry) is true.
Notice:
1) The value of the relational expression tom == jerry is false. This is because strings are objects, so tom and jerry are stored in references.
2) The string object calls public boolean equalsIgnoreCase(String s) to compare whether the current string object is the same as the string specified by parameter s. The size is ignored during comparison.
The string object calls the startsWith(String s) method to determine whether the prefix of the current string object is the string specified by parameter s, for example:
Stringtom=weather forecast, cloudy and light rain, jerry=match result, Chinese team won;
Then, the value of tom.startsWith(weather) is true, and the value of jerry.startsWith(weather) is false.
Use the endsWith(String s) method to determine whether the suffix of a string is the string s, for example:
The value of tom.endsWith(Heavy Rain) is false, and the value of jerry.endsWith(Victory) is true.
String objects can be compared in lexicographic order with the string specified by parameter s using the compareTo(String s) method in the String class. If the current string is the same as s, this method returns a value of 0; if the current string object is greater than s, this method returns a positive value; if it is less than s, this method returns a negative value.
For example, the sorting position of character a in the Unicode table is 97, and the sorting position of character b is 98, then for:
Stringstr=abcde;
str.compareTo(boy) is less than 0, str.compareTo(aba) is greater than 0, str.compareTo(abede) is equal to 0.
To compare two strings lexicographically, you can also use the public int compareTolgnoreCase(String s) method, which ignores case.