Home >Java >javaTutorial >Java classic string comparison method: compareToIgnoreCase()

Java classic string comparison method: compareToIgnoreCase()

Y2J
Y2JOriginal
2017-05-17 10:42:402044browse

compareToIgnoreCase() Method is used to compare two strings in dictionary order, 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 If it is less than the string parameter, a value less than 0 will be returned;

If the string is greater than the string parameter, a value greater than 0 will be returned.

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

[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. Go deeper Understand the differences between compareTo and comparetoIgnorecase

6. Detailed explanation of the principle of the return value of compareToIgnoreCase

The above is the detailed content of Java classic string comparison method: 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