beginnersbook.com

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

Java – String equals() and equalsIgnoreCase() Methods example

By Chaitanya Singh | Filed Under: String handling

In this tutorial we will discuss equals() and equalsIgnoreCase() methods. Both of these methods are used for comparing two strings. The only difference between them is that the equals() methods considers the case while equalsIgnoreCase() methods ignores the case during comparison. For e.g. The equals() method would return false if we compare the strings “TEXT” and “text” however equalsIgnoreCase() would return true.

boolean equals(String str): Case sensitive comparison
boolean equalsIgnoreCase(String str): Case in-sensitive comparison

Example 1: equals()

public class EqualsExample1{
   public static void main(String args[]){
       String str1= new String("Hello");
       String str2= new String("Hi");
       String str3= new String("Hello");
       System.out.println("str1 equals to str2:"+str1.equals(str2));
       System.out.println("str1 equals to str3:"+str1.equals(str3));
       System.out.println("str1 equals to Welcome:"+str1.equals("Welcome"));
       System.out.println("str1 equals to Hello:"+str1.equals("Hello"));
       System.out.println("str1 equals to hello:"+str1.equals("hello"));
   }
}

Output:

str1 equals to str2:false
str1 equals to str3:true
str1 equals to Welcome:false
str1 equals to Hello:true
str1 equals to hello:false

Example2: equalsIgnoreCase()

public class EqualsExample2{
   public static void main(String args[]){
       String str1= new String("Apple");
       String str2= new String("MANGO");
       String str3= new String("APPLE");
       System.out.println("str1 equals to str2:"+str1.equalsIgnoreCase(str2));
       System.out.println("str1 equals to str3:"+str1.equalsIgnoreCase(str3));
       System.out.println("str1 equals to Welcome:"+str1.equalsIgnoreCase("Welcome"));
       System.out.println("str1 equals to Apple:"+str1.equalsIgnoreCase("Apple"));
       System.out.println("str2 equals to mango:"+str2.equalsIgnoreCase("mango"));
   }
}

Output:

str1 equals to str2:false
str1 equals to str3:true
str1 equals to Welcome:false
str1 equals to Apple:true
str2 equals to mango:true

Enjoyed this post? Try these related posts

  1. Java – String toLowerCase() and toUpperCase() Methods
  2. Java – String toCharArray() Method example
  3. Java String join() method explained with examples
  4. Java – String Class and its methods explained with examples
  5. Java String format() method explained with examples
  6. Java String compareTo() Method with examples

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 – 2019 BeginnersBook . Privacy Policy . Sitemap