beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java String compareToIgnoreCase() Method example

By Chaitanya Singh | Filed Under: String handling

The Java String compareToIgnoreCase() method compares two strings lexicographically and returns 0 if they are equal. As we know compareTo() method does the same thing, however there is a difference between these two methods. Unlike compareTo() method, the compareToIgnoreCase() method ignores the case (uppercase or lowercase) while comparing strings.

Java String compareToIgnoreCase() Method

Method Signature:

int compareToIgnoreCase(String str)

For example:

String s1 = "BEGINNERSBOOK"; //uppercase
String s2 = "beginnersBOOK"; //mixedcase
s1.compareTo(s2); //this would return non-zero value
s1.compareToIgnoreCase(s2); //this would return zero

Similar to compareTo() method, the compareToIgnoreCase() method compares the strings based on the Unicode value of their each character. It returns 0 when the strings are equal otherwise it returns positive or negative value.

Java String compareToIgnoreCase() example

In the following example, we have three strings, all the three strings are same but their letter case is different. string1 is in uppercase, string2 is in lowercase and string3 is a mix of uppercase and lowercase letters. We are using compareToIgnoreCase() method to compare these strings.

public class CompareExample {
   public static void main(String args[]) {
	String string1 = "HELLO";
	String string2 = "hello";
	String string3 = "HellO";

	int var1 = string1.compareToIgnoreCase(string2);
	System.out.println("string1 and string2 comparison: "+var1);

	int var2 = string1.compareToIgnoreCase(string3);
	System.out.println("string1 and string3 comparison: "+var2);

	int var3 = string1.compareToIgnoreCase("HeLLo");
	System.out.println("string1 and HeLLo comparison: "+var3);
   }
}

Output:

string1 and string2 comparison: 0
string1 and string3 comparison: 0
string1 and HeLLo comparison: 0

Java String compareToIgnoreCase() vs compareTo() example

Lets take an example to understand the difference between these two methods. Here we are comparing two strings, which are same but their letter case is different. One of the string is in uppercase and the second string is in lowercase.

public class JavaExample {
   public static void main(String args[]) {
	//uppercase
	String str1 = "HELLO";
	//lowercase
	String str2 = "hello";

	System.out.println(str1.compareTo(str2));
	System.out.println(str1.compareToIgnoreCase(str2));
   }
}

Output:
Java String compareToIgnoreCase method example

❮ PreviousNext ❯

Comments

  1. Sachin Koparde says

    October 8, 2018 at 1:38 PM

    what if there are two different strings with different case?

    Reply
    • Chaitanya Singh says

      December 22, 2018 at 2:03 PM

      Obviously the method would not return 0.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorials

  • Learn Java
  • OOPs Concepts
  • Java Collections

Java String

  • Java String

Java String Methods

  • String charAt()
  • String compareTo()
  • String compareToIgnoreCase()
  • String contains()
  • String concat()
  • substring
  • String valueOf()
  • String startsWith()
  • String equals()
  • String format()
  • String endsWith()
  • String indexOf()
  • String lastIndexOf()
  • String length()
  • String replace()
  • String split()
  • String trim()
  • String intern()
  • String isEmpty()
  • String matches()
  • String regionMatches()
  • String contentEquals()
  • String toCharArray()
  • String getBytes()
  • String join()
  • String getChars()
  • String copyValueOf()

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap