Equals() and EqualsIgnoreCase() Method In Java Experimented And Explained (Java String Methods)

 Equals() and EqualsIgnoreCase() Method In Java Experimented And Explained (Java String Methods)


There are various predefined string functions(methods) in Java.

In this post we will explore two methods : equals() and equalsIgnoreCase().

equals() :

This method compares two string and returns true if both the strings are equal and returns false if both the strings are not equal.

Note : equals() is case sensitive i.e "A" and  "a" are not equal.

Syntax :  string1.equals(string2);

equalsIgnoreCase() :

This methos compares two string and returns true if both the strings are equal and returns false if both the strings are not equal.

The difference between equals() and equalsIgnoreCase() is that equalsIgnoreCase() ignores the case i.e "A"  and "a" are equals in equalsIgnoreCase() method.

Syntax : string1.equalsIgnoreCase(string2);


Java Example Code :


public class EqualsAndEqualsIgnoreCase 
{

public static void main(String[] args) 
{
String s1="Learn Programming";
String s2="learn programming";

//equals()

System.out.println(s1.equals(s2));  //returns false since equals will check case too i.e l and p mismatching

System.out.println(s1.equals("Learn Programming"));  //returns true since both are equal

System.out.println(s1.equals("Learn"));  //returns false 
//equalsIgnoreCase()

System.out.println(s1.equalsIgnoreCase(s2));  //returns true since it ignores case 

System.out.println(s1.equalsIgnoreCase("Learn Programming"));  //return true 

System.out.println(s1.equalsIgnoreCase("learn"));  //returns false
}

}



Output :



Contains Method In Java Explored And Experimented (String Functions In Java)

Contains Method In Java Explored And Experimented (String Functions In Java)


There are various inbuild string handling methods in java. 
In this post we will discuss about the .contains() method in java.


.contains() method will check whether a sequence of characters is in the given string.
This Method is Case sensitive.
 


Java Example Code :

public class ContainsMethod 
{
public static void main(String[] args) 
{
String s1="lets learn programming";
String s2="lets learn programmingg";
//1. contains() -> check whether string contains the given sequence of characters
System.out.println(s1.contains("Programming"));  // it return false because Programming and programming are not same i.e case sensitive
System.out.println(s1.contains("programming"));  // it return true because programming and programming are same
System.out.println(s1.contains("gram"));  // it return true because there is gram in programming
System.out.println(s1.contains("ts learn"));  // it return true because lets learn programming has ts learn in sequence


System.out.println(s1.contains(s2));  // it return false because there is extra g in s2 

System.out.println(s2.contains(s1));  // it return true because there is lets learn programming(s1) in lets learn programmingg(s2)

}
}


Output :




Popular Posts