String Comparison

The String class contains a methods to compare two Strings or Sub strings within strings.

equals( ) and equalsIgnoreCase( )

equals()

  • To compare two strings for equality equals() method is used with the following general form:

boolean equals(Object str)

str- is the String object being compared with the invoking String object.

  • It returns true if the strings contain the same characters in the same order , and false otherwise.
  • This comparison is case sensitive.

equalsIgnoreCase( )

  • To perform comparison that ignores case sensitive means a-z is same as A-Z we used this function with the following general form

boolean equalsIgnoreCase(String str)

str-is the String object being compared with the invoking String object.

  • It returns true i f the strings contain the same characters in the same order , and false otherwise.
// Demonstrate equals() and equalsIgnoreCase().
public class equalsDemo 
{
 public static void main(String args[]) 
 {
 String s1 = "Hello";
 String s2 = "Hello";
 String s3 = "Good-bye";
 String s4 = "HELLO";
 System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
 System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
 System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
 System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
 }
}

Output:

Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

regionMatches( )

  • This method compares a specific region inside a string with another specific region in another string.

boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars)

  • We have an overloaded method to ignore the case sensitive while comparing.

boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)

startIndex- specifies the index at which the region begins within the invoking String object.

str2-Specifies the String being compared.

str2StartIndex-specifies the index at which the comparison will start within str2.

numChars-The length of the substring being compared is passed.

ignoreCase-It is true, the case of the characters is ignored. Otherwise, false case is significant.

// Demonstrate regionMatches() with ignore case also
public class RegionMatches
{
 public static void main(String args[]) 
 {
 String s1 = "Hello";
 String s2 = "Hello";
 String s3 = "Good-bye";
 String s4 = "HELLO";
 System.out.println(s1 + " region matches " + s2 + " -> " + s1.regionMatches(0,s2,0,5));
 System.out.println(s1 + " region matches " + s3 + " -> " + s1.regionMatches(2,s3,4,3));
 System.out.println(s1 + " region matches" + s4 + " -> " + s1.regionMatches(0,s4,0,5));
System.out.println(s1 + "  region matches IgnoreCase " + s4 + " -> " + s1.regionMatches(true,0,s4,0,5));
 }
}

Output:

Hello region matches Hello -> true
Hello region matches Good-bye -> false
Hello region matchesHELLO -> false
Hello region matches IgnoreCase HELLO -> true

startsWith( ) and endsWith( )

  • These methods are specified forms of regionMatches().
  • startsWith( ) method determines whether a given String begins with a specified string or not.

  • endsWith( ) determines whether the String in question ends with a specified string or not.

General forms are:

  • boolean startsWith(String str)
  • boolean endsWith(String str)

    str is the String being tested.If the string matches, true is returned. Otherwise ,false is returned.

  • A second form of startsWith(), which specifies a starting point:

    boolean startsWith(String str, int startIndex)

  • startIndex specifies the index into the invoking string at which point the search will begin.

// Demonstrate startsWith() and endsWith()
public class StartsEndsWithDemo 
{
 public static void main(String args[]) 
 {
 String s1 = "Hello World";
 String s2 = "Hello";
 String s3 = "World";
 String s4 = "HELLO";
 System.out.println(s1 + " stratsWith() " + s2 + " -> " + s1.startsWith(s2));
 System.out.println(s1 + " endsWith() " + s3 + " -> " + s1.endsWith(s3));
 System.out.println(s1 + " startsWith() " + s4 + " -> " + s1.startsWith(s4));
System.out.println(s1 + "  startsWith() with start index " + s4 + " -> " + s1.startsWith("llo",2));
 }
}

Output:

Hello World stratsWith() Hello -> true
Hello World endsWith() World -> true
Hello World startsWith() HELLO -> false
Hello World startsWith() with start index HELLO -> true

equals( ) Versus ==

  • There is difference between equals() and ==
  • As equals() will compare two strings whether they have same sequence of characters or not.
  • == compares two object references to see whether they refer to the same instance or not.
// equals() vs ==
public class EqualsNotEqualTo
{
 public static void main(String args[]) 
 {
   String s1 = "Hello";
 String s2 = new String(s1);
 String s3 = s1;
 System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2));
 System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
    System.out.println(s1 + " == " + s3 + " -> " + (s1 == s3));
 }
}

Output:

Hello equals Hello -> true

Hello == Hello -> false

Hello == Hello -> true

compareTo( )

  • In applications there is a need to find which string is less or greater or equals depends on the order that comes in the dictionary.
  • This method allows us to do that with the following general form:

int compareTo(String str)

  • str is the String being compared with the invoking String.
  • The result it returns is as follows

  • We can compare two strings with ignoring case by using following general form:

    int compareToIgnoreCase(String str)

Value Meaning
Less than Zero The invoking string is less than str
Greater than Zero The invoking Sting is greater than str
Equals to Zero The two strings are same.
// A bubble sort for Strings.
public class SortString 
{
 static String arr[] = { "Now", "is", "the", "time", "for", "all", "good", "men", "to", "come", "the", "aid", "of", "their", "country" };

 public static void main(String args[]) 
 {
 for(int j = 0; j < arr.length; j++) 
 {
 for(int i = j + 1; i < arr.length; i++) 
 {
 if(arr[i].compareTo(arr[j]) < 0) {
 String t = arr[j];
   arr[j] = arr[i];
 arr[i] = t;
 }
 }
 System.out.print(arr[j]+" ");  //As Now starts with caps so it have less ASCII values so it will come first

 }
  System.out.println();
   System.out.println("with ignore case");
   for(int j = 0; j < arr.length; j++) 
 {
 for(int i = j + 1; i < arr.length; i++) 
 {
 if(arr[i].compareToIgnoreCase(arr[j]) < 0) {
 String t1= arr[j];
   arr[j] = arr[i];
 arr[i] = t1;
 }
 }
 System.out.print(arr[j]+" ");
 }
 }
}

Output:

Now aid all come country for good is men of the the their time to

with ignore case

aid all come country for good is men Now of the the their time to

results matching ""

    No results matching ""