String Class Method in Java: Session 4

Before reading this article, please go through the following previous parts of the series:
We have now arrived at the point to get some knowledge about Java string class methods. To fully understand, let's move forward to learn about other methods by the same easy and simple way.

Method: Java String compareTo()


This method is basically used to compare two strings that may be different or the same. It compares both the strings lexicographically.

A Lexicographical comparison compares each character of both the strings converted into their Unicode values for comparison. If the strings are equal (difference of Unicode values) then it returns 0 (zero) and if the first string is lexicographically greater than the second string then it returns a positive value otherwise a negative value.

It has the following two variants:
  1. Java String compareTo(String str).
  2. Java String compareTo(Object obj).

The first one does the comparison of the two string literals.

String1.compareTo(String2) where String1 and String2 are string literals.

The second one does the comparison between a string and an object.

String1.compareTo(String object) where String1 is a literal whose value would be compared with the string specified in the method argument.

Let's go through the example.

Example

The following example shows outputs for both the variants.   

  1. public class CompareTo   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="Apple is a fruit";  
  6.       String str2="Apple is also a brand";  
  7.       String str3="Apple is a fruit";  
  8.       System.out.println("After string to string comparison we get: "+str2.compareTo(str1));//positive  
  9.       System.out.println("After string to string comparison we get: "+str1.compareTo(str3));//zero
  10.       System.out.println("After string to string comparison we get: "+str1.compareTo(str2));//negative 
  11.       System.out.println();  
  12.       System.out.println("After string to object comparison we get: "+str2.compareTo("Apple is a fruit"));//positive  
  13.       System.out.println("After string to object comparison we get: "+str1.compareTo("Apple is a fruit"));//zero
  14.       System.out.println("After string to object comparison we get: "+str1.compareTo("Apple is also a brand"));//negative
  15.   }  
  16. }  
Output

compare

Method: Java String compareToIgnoreCase()


This method is quite similar to the preceding method but with the difference that this method ignores the cases (uppercase and lowercase). It also compares lexicographically as does the preceding method and returns the same values for the preceding specified condition.

Example

In this example, we took three strings s1=lowercase, s2=uppercase, s3=mixedcase to compare these three strings.
  1. public class CompareTo   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="love programming";  
  6.       String str2="LOVE PROGRAMMING";  
  7.       String str3="Love Programming";  
  8.       System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str1));//equal strings hence returns zero  
  9.       System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str2));//ignores case sensitivity hence returns zero  
  10.       System.out.println("After string to string comparison we get: "+str1.compareToIgnoreCase(str3));//ignores case sensitivity hence returns zero  
  11.       System.out.println();  
  12.       System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("love programming"));//equal strings hence returns zero  
  13.       System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("LOVE PROGRAMMING"));//ignores case sensitivity hence returns zero  
  14.       System.out.println("After string to object comparison we get: "+str1.compareToIgnoreCase("Love Programming"));//ignores case sensitivity hence returns zero  
  15.   }  
  16. }  
Output

compareto

In the preceding output, we can observe that the method ignores the case sensitivity and returns zero (0) for all the comparisons.

Method: Java String concat()


This method is used to combine two or more strings to produce a combined single string. It combines the specified string at the end of the given string.

This method can be used more than one time in a single statement to produce a big string. This is like appending another string to one string.

Example

The following example showsthat there are four possible ways to concatenate by using various strings.
  1. public class Concat   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="India";  
  6.       String str2=" is a very";  
  7.       String str3=" beautiful country";  
  8.       String str4="I love ";  
  9.       // one way of concatenation  
  10.       System.out.println("After string to string concatenation we get: "+str4.concat( str1));//add 'India' after 'I love'  
  11.       System.out.println("After string to string concatenation we get: "+str1.concat( str3));//add 'beautiful country' after 'India'  
  12.       //second way of concatenation  
  13.       System.out.println("After string to string concatenation we get: "+str1.concat( str2.concat( str3)));//returns 'India is a very beautiful country'  
  14.       //third way of concatenation  
  15.       System.out.println("After string to string concatenation we get: "+str4.concat(" my").concat(" country").concat(" very much "));  
  16.       //returns 'I love my country very much'  
  17.       //fourth way of concatenation.  
  18.       String str5="Welcome";  
  19.       str5=str5.concat(" to this");  
  20.       str5=str5.concat(" article");  
  21.       System.out.println("After string to string concatenation we get: "+str5);  
  22.   }  
  23. }  
Output

concat

Method: Java String contains()


When the sequence of characters needs to be found, then this method is very helpful. This method searches the character sequence in the given string. It returns values in Boolean, in other words, true or false. If the sequence of characters is found then it returns true otherwise false.

Note: If the sequence of characters is found null then it throws an exception, in other words

NullPointerException().

Let's see the example.

Example

The example contains three possible ways to check the character sequences.

  1. public class Contains   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="Delhi is the capital of India";  
  6.       //one way to find charactersequence  
  7.       System.out.println("Does str1 contains 'capital'..?: "+str1.contains("capital"));  
  8.       System.out.println("Does str1 contains 'mahal is'..?: "+str1.contains("mahal is"));  
  9.       //second way to find charactersequence  
  10.       boolean find1=str1.contains("cap");  
  11.       boolean find2=str1.contains("Taj");  
  12.       boolean find3= str1.contains("c");  
  13.       boolean find4= str1.contains("f");  
  14.       System.out.println("Does str1 contains 'cap'..?: "+find1);  
  15.       System.out.println("Does str1 contains 'taj'..?: "+find2);  
  16.       System.out.println("Does str1 contains 'c'..?: "+find3);  
  17.       System.out.println("Does str1 contains 'f'..?: "+find4);  
  18.       //third way to find charactersequence  
  19.       CharSequence cs1="tal";  
  20.       CharSequence cs2="hal";  
  21.       boolean verify1=str1.contains(cs1);  
  22.       boolean verify2=str1.contains(cs2);  
  23.       System.out.println("Does str1 contains 'tal'..?: "+verify1);  
  24.       System.out.println("Does str1 contains 'hal'..?: "+verify2);  
  25.   }  
  26. }  
Output


To continue further please follow the link below.
 

Thank you, keep learning and sharing. 


Similar Articles