Java String compareToIgnoreCase() method with example

Java String compareToIgnoreCase() method compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by calling Character.toLowerCase(Character.toUpperCase(character)) on each character.

Note that this method does not take locale into account, and will result in an unsatisfactory ordering for certain locales. The java.text package provides collators to allow locale-sensitive ordering.

Method Signature

public int compareToIgnoreCase(String str)

Parameters:

str – the String to be compared.

Returns:

a negative integer, zero, or a positive integer as the specified String is greater than, equal to, or less than this String, ignoring case considerations.

Since:

1.2. See Also:Collator.compare(String, String)

String compareToIgnoreCase() method Implementation

public int compareToIgnoreCase(String str) {
        return CASE_INSENSITIVE_ORDER.compare(this, str);
    }

Java String compareToIgnoreCase() method example

package com.ehowtonow.java.lang.string;

public class CompareToIgnoreCaseExample {

	public static void main(String[] args) {

		String s1="ehowtonow";  
		String s2="eHowToNow";
		String s3="";
		String s4 = new String("EHOWTONOW");
		System.out.println(s1.compareToIgnoreCase(s2));
		System.out.println(s3.compareToIgnoreCase(s2));
		System.out.println(s1.compareToIgnoreCase(s3));
		System.out.println(s1.compareToIgnoreCase(s4));
	
	}

}

Output

0
-9
9
0


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java String codePointAt() method with example
  2. Java String concat() method with example
  3. How to search a word inside String
  4. Java String compareTo() method with example
  5. How to convert a String to Lower case
  6. How to format the String by using Locale
  7. Java String compareToIgnoreCase() method with example
  8. Java String Compare
  9. Remove the particular character from string
  10. Java String indexOf(String str, int fromIndex) method with example
  11. How to get Character unicode point in a String
  12. Java String charAt() method with example
  13. How to Split the String in Java
  14. Java String codePointBefore() method with example
  15. How to convert a String to Upper case

Be the first to comment

Leave a Reply

Your email address will not be published.


*