Analyzing single string

In java programming, we will occasionally have problems comparing string size, compareTo () method is very simple to implement this functionality. The method for determining whether a string is greater than, equal to, or less than another string. Determination is based on size of the string according to their order in the dictionary determined.

语法:Str1.compareTo(Str2);

其返回的是一个int类型值。若Str1等于参数字符串Str2字符串,则返回0;若该Str1按字典顺序小于参数字符串Str2,则返回值小于0;若Str1按字典顺序大于参数字符串Str2,则返回值大于0。

java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值,看下面一组代码

String a="a",b="b";
System.out.println(a.compareto.b);
则输出-1;
若a="a",b="a"则输出0;
若a="b",b="a"则输出1;

This is more a single character, if a long string of it? ?
If a = "ab", b = "b", the output 1;
if a = "abcdef", b = "b" , the output 1;
that is, if the first letter of two different strings, the asc difference method returns the first letter code;

If the same initials it? ?
If a = "ab", b = "a", the output 1;
if a = "abcdef", b = "a" output 5;
if a = "abcdef", b = "abc" Output 3;
if a = " abcdef ", b =" ace "output 1;
i.e., if the comparison involved in the first two character strings the same, the next character until far different, the difference in return code asc different characters, if two string is not as long, and the character can participate in exactly the same comparison, the difference between the length of the two strings is returned

Released five original articles · won praise 0 · Views 41

Guess you like

Origin blog.csdn.net/xfolive/article/details/104387308