Home  >  Article  >  Java  >  Let's talk about how to use Java compareToIgnoreCase

Let's talk about how to use Java compareToIgnoreCase

巴扎黑
巴扎黑Original
2017-06-10 17:29:492266browse

This article will give you a detailed introduction to the use of Java compareToIgnoreCase. I hope it will be helpful to friends in need!

Let's talk about how to use Java compareToIgnoreCase

Java compareToIgnoreCase() method

compareToIgnoreCase() method is used to compare two lexicographically String, regardless of case.

Syntax

int compareToIgnoreCase(String str)

Parameters

  • str -- The string to be compared.

Return value

  • If the parameter string is equal to this string, the return value is 0;

  • If this string is less than the string parameter, return a value less than 0;

  • If this string is greater than the string parameter, return a value greater than value of 0.

Example

public class Test {

    public static void main(String args[]) {
        String str1 = "STRINGS";
        String str2 = "Strings";
        String str3 = "Strings123";

        int result = str1.compareToIgnoreCase( str2 );
        System.out.println(result);
      
        result = str2.compareToIgnoreCase( str3 );
        System.out.println(result);
     
        result = str3.compareToIgnoreCase( str1 );
        System.out.println(result); 
    }
}

The execution result of the above program is:

0
-3
3

Explanation:

Regarding the compareToIgnoreCase() method, no matter whether the parameter is an object or a string, what is ultimately compared is the difference between the two strings. Hereinafter, the one on the calling method side is called the original string, and the one in the method parameter is the parameter string. .

This method is divided into two comparison methods:

1. When different characters are within the shorter string length

Return value = original string and parameter character The ASCII code value of the first different character in the string is the original minus parameter.

The example is as follows:

String str1="javDscrspt";
String str2="jAvascript";
str1.compareToIgnoreCase(str2);

The return value at this time is 3, which is the ASCII code value of d (100) minus the ASCII code value of a (97) or the difference between D and A. .

Note: Only compare the first character that is different (the case of letters is not considered in this method). The following s and i are also different but will not be compared. It has nothing to do with the case of letters, so only the same characters are compared. The difference in ASCII code value in the format.

2. When different characters are outside the length of the shorter string

Return value = the number of characters that differ between the original string and the parameter string. It is positive when the length of the original string is large. , otherwise it is negative.

The example is as follows:

String str1="javAScript";
String str2="JaVa";
str1.compareToIgnoreCase(str2);

The return value at this time is 6, which is the number of extra characters in str1 compared to str2.

Note: At this time, only the number of digits is compared, not the ASCII code value. It is not the ASCII code value of S(s) minus the ASCII code value of 0. The characters in front of the parameter string are the same as the original string. When, the return value is the number of characters that differ between the two. Even changing the following characters will not affect the returned value. For example, if String str1="jAva233666", the result is still 6.

The above is the detailed content of Let's talk about how to use Java 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