Home >Common Problem >How do strings compare in size?

How do strings compare in size?

不言
不言Original
2019-03-05 15:50:42169669browse

How do strings compare in size?

Character comparison refers to the operation of comparing the size of a single character or string in dictionary order. Generally, the size of the ASCII code value is used as the standard for character comparison.

When comparing strings, the size of the strings is compared starting from the first character on the left. The larger one is larger and the smaller one is smaller. If they are equal, continue to compare the following characters;

For example, if ABC and ACDE are compared, if the first character is the same, continue to compare the second character. Since the second character is larger than the next string, the comparison will not continue. The result is that the latter string is larger. Another example is the comparison between ABC and ABC123. After comparing three characters, the first string ends, so the next string is larger.

So, the length cannot directly determine the size. The size of the string is determined by the frontmost character from the left.

When comparing two strings, pay attention to the following points:

1. When comparing two strings of different lengths, the longer string is not necessarily "big". For example, A$ is "ABCE" and B$ is "ABCDEF". When comparing A$ with B$, the 4th character of A$ is "E", the 4th character of B$ is "D", and "D"<"E", so B$>A $. Even though B$ is longer than A$.

2. When the string contains spaces, spaces are also included in the comparison. For example, A$-"t-ABOOK"u (indicates a space), B$-"A-BOOK", displays A$>B$.

3. There is a difference between the ASCII code values ​​of uppercase letters and lowercase letters, so "yes" > "YEs".

4. When a string is composed entirely of uppercase (or lowercase) English letters, the order of the strings is the same as their order in the dictionary.

5. Strings composed of Chinese characters can be compared. Such as "Li Hong" <"Wang Jun". Their size is actually determined by the size of the string composed of their pinyin. The above example is: "LIHONG"<"WANGJUN".

The above is the detailed content of How do strings compare in size?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn