How to compare string size

describe

The comparison of the string size is not directly determined by the length of the string, but is compared from the first character on the left. The larger one is the larger one, and the smaller one is the small one. If they are equal, then continue to compare the following characters in the order of the strings , the main scenarios involved are as follows:

Common scenarios and descriptions for comparing strings


1. The lengths of the two strings to be compared are not the same. If it is not a long string, it must be "big".

Such as: For example, the string c="EFG" and the string, d="EAFG" The second character is 'F' and 'A', so C>D.
2. The ASCII code values ​​of uppercase letters and lowercase letters are different, so "good">"GOOD".
3. When the strings are all composed of uppercase (or lowercase) English letters, the size order of the strings is the same as their order in the dictionary.
4. Strings composed of Chinese characters can participate in the comparison. Such as "An Qi"<"Zhang San". Their size is actually determined by the size of the character string formed by their pinyin. The above example is: "ANQI"<"ZHANGSAN".
5. When the string has spaces, the spaces also participate in the comparison. Such as "abcd" > "a bcd

compareTo() provided by java to determine the size of the string

The compareTo() method in Java returns the difference between the ASCII codes of the two strings before and after the comparison

The compareTo() method is very simple to achieve this function. This method is used to determine whether a string is greater than, equal to, or less than another string. The basis for judging the size of strings is based on their order in the dictionary.

       Syntax: str1.compareTo(str2);

It returns a value of type int.

If Str1 is equal to the parameter string Str2, return 0;

If the Str1 is lexicographically less than the parameter string Str2, the return value is less than 0;

If Str1 is lexicographically greater than the parameter string Str2, the return value is greater than 0.


————————————————
Original link: https://blog.csdn.net/FJJ543/article/details/99728140
Original link: https://blog.csdn.net/weixin_37962206 /article/details/121655942

Guess you like

Origin blog.csdn.net/weixin_70280523/article/details/131633379