Home > Java > javaTutorial > body text

Detailed introduction to compareToIgnoreCase() method

Y2J
Release: 2017-05-17 10:45:06
Original
3680 people have browsed it

Java String.compareToIgnoreCase() method usage example tutorial, compare two strings, dictionaries, ignore case differences

Description

java.lang.String.compareToIgnoreCase() Method compares two strings, dictionaries, ignoring case differences.

Statement

The following is the declaration of the java.lang.String.compareToIgnoreCase() method

public int compareToIgnoreCase(String str)

Parameters

str -- This is the string to be compared.

Return value

This method returns a negative integer , zero or a positive integer if the specified string is greater than, equal to or less than this string, ignoring case considerations.

Exception

NA

##Example

The following example illustrates how to use the java.lang.String.compareToIgnoreCase() method

package com.yiibai;
import java.lang.*;
public class StringDemo {
  public static void main(String[] args) {
  
    String str1 = "tutorials", str2 = "TUTORIALS";
    // comparing str1 and str2 with case ignored
    int retval = str1.compareToIgnoreCase(str2);
    // prints the return value of the comparison
    if (retval > 0) {
       System.out.println("str1 is greater than str2");
    }
        
    else if (retval == 0) {
       System.out.println("str1 is equal to str2");
    }
        
    else {
       System.out.println("str1 is less than str2");
    }
  }
}
Copy after login

Let us compile and run the above program, which will produce the following results:

str1 is equal to str2
Copy after login

【 Related recommendations】

1.

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

2.

Java Free Video Tutorial

3.

compareToIgnoreCase() compares two strings without case sensitivity

4.

Java Classic string comparison method: compareToIgnoreCase()

5.

In-depth understanding of the differences between compareTo and comparetoIgnorecase

6.

Detailed explanation The principle of return value of compareToIgnoreCase

The above is the detailed content of Detailed introduction to compareToIgnoreCase() method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!