Home  >  Article  >  Java  >  Detailed explanation of the principle of return value of compareToIgnoreCase

Detailed explanation of the principle of return value of compareToIgnoreCase

Y2J
Y2JOriginal
2017-05-17 10:31:452468browse

int n1="This".compareToIgnoreCase("tHe");
int n2="JAVA".compareToIgnoreCase("javaline")
int n3="Java".compareToIgnoreCase("Javits")

The result is n1=4,n2=-4,n3=-8,

Why is this? The reason is this:

n1: Comparing "This" with "tHe", starting from the third character, i is 4 larger than e.
n2: The first four characters are the same,

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value:
this.length()-anotherString.length()
n3: Same as n1

【Related recommendations】

1. Special recommendation"php Programmer Toolbox" V0.1 version download

2. Java Free Video Tutorial

3. Detailed introduction to compareToIgnoreCase() method

4.compareToIgnoreCase() compares two strings without case sensitivity

5.Java classic string comparison method: compareToIgnoreCase()

6.Go deeper Learn the difference between compareTo and comparetoIgnorecase

The above is the detailed content of Detailed explanation of the principle of return value of compareToIgnoreCase. 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