String compareToIgnoreCase() method in Java

The compareToIgnoreCase(String str) method in Java compares two strings lexicographically, ignoring case differences.

Syntax

Let us see the syntax,

int compareToIgnoreCase(String str)

Parameters

Let us see the parameters,

  • str – the comparison string

Example

The following is an example of compareToIgnoreCase(String str),

public class StudyopediaDemo {

   public static void main(String args[]) {
	 
     int value;
     String msg1 = "Welcome to USA!";
     String msg2 = "Welcome to USA!";
	 
     String msg3 = "Welcome to Australia!";
     String msg4 = "Welcome to AUSTRALIA";
	 
     System.out.println("String one: " +msg1);
     System.out.println("String two: " +msg2);
     System.out.println("String three: " +msg3);
     System.out.println("String four: " +msg4);
      
     value = msg1.compareToIgnoreCase( msg2 );
     System.out.println(value);
	 
     value = msg2.compareToIgnoreCase( msg3 );
     System.out.println(value);
	 
     value = msg3.compareToIgnoreCase( msg4 );
     System.out.println(value);
  } 
}

Output

The following is the output,
Java String comparetoignorecase method

String compareTo(String anotherString) method in Java
String concat() method in Java
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment