String Class Methods in Java: Session 5

Before reading this article, please go through the following previous parts of the series:

Method: Java String equals()

 

We will divide this method into its three variants to get a clear understanding of all the equals() methods. Its variants are as in the following.

  • Java String equals(String str)
  • Java String equalsIgnoreCase(String str)
  • Java String contentEquals(StringBuffer strbuf)

All the three variants are used to compare the two strings but they all have slight differences, let's move forward and explain them.

The Java String equals(String str) method compares two strings. It returns Boolean values true or false as output. This method considers the content as well as the string cases (upper and lower case) for comparison. It returns true only if both the content and the case of the string matches, otherwise it returns false. The String equals() method overrides the equals() method of the object class.

The Java String equalsIgnoreCase() method is approximately similar to the preceding variant but with the difference that it does not consider string cases and the rest is the same as in the preceding variant.

The Java String contentEquals() method is usually similar to equals() but with the difference that it only considers the content of the string. If the content of the strings matches, it returns true otherwise false.

Let's use an example to understand all the three variants together using various strings.

Example

  1. public class EqualsVariants   
  2. {  
  3.    public static void main(String args[])  
  4.    {  
  5.       String str1="See you later";  
  6.       String str2="Come Back Soon";  
  7.       String str3="SEE YOU LATER";  
  8.       String str4="See you later";  
  9.       System.out.println("Outputs for equals()");  
  10.       System.out.println("Does str1 equals to str2..?: "+str1.equals(str2));//content not same hence returns false  
  11.       System.out.println("Does str1 equals to str3..?: "+str1.equals(str3));//case not same hence returns false  
  12.       System.out.println("Does str1 equals to str4..?: "+str1.equals(str4));//content and case both are same hence returns true  
  13.       System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.equals("Come Back Soon"));//false  
  14.       System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.equals("SEE YOU LATER"));//false  
  15.       System.out.println("Does str1 equals to 'See you later'..?: "+str1.equals("See you later"));//true  
  16.       System.out.println("Outputs for equalsIgnoreCase()");  
  17.       System.out.println("Does str1 equals to str2..?: "+str1.equalsIgnoreCase(str2));//content not same hence returns false  
  18.       System.out.println("Does str1 equals to str3..?: "+str1.equalsIgnoreCase(str3));//case is not same hence by ignoring the case it returns true  
  19.       System.out.println("Does str1 equals to str4..?: "+str1.equalsIgnoreCase(str4));//content and case both are same hence returns true  
  20.       System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.equalsIgnoreCase("Come Back Soon"));//false  
  21.       System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.equalsIgnoreCase("SEE YOU LATER"));//true  
  22.       System.out.println("Does str1 equals to 'See you later'..?: "+str1.equalsIgnoreCase("See you later"));//true  
  23.       //for contentEquals() we have to StringBuffers of above strings as it works with StringBuffer  
  24.       StringBuffer str5= new StringBuffer("See you later");  
  25.       StringBuffer str6= new StringBuffer("Come Back Soon");  
  26.       StringBuffer str7= new StringBuffer("SEE YOU LATER");  
  27.       System.out.println("Outputs for contentEquals()");  
  28.       System.out.println("Does str1 equals to str6..?: "+str1.contentEquals(str6));//content not same hence returns false  
  29.       System.out.println("Does str1 equals to str7..?: "+str1.contentEquals(str7));//case is not same hence returns false  
  30.       System.out.println("Does str1 equals to str5..?: "+str1.contentEquals(str5));//content and case both are same hence returns true  
  31.       System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.contentEquals("Come Back Soon"));//false  
  32.       System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.contentEquals("SEE YOU LATER"));//false  
  33.       System.out.println("Does str1 equals to 'See you later'..?: "+str1.contentEquals("See you later"));//true  
  34.    }  
  35. }  
Output


In the preceding output, we can observe the differences in the outputs of all the three variants discussed above.

Method: Java String format()

 

This method helps to format the specified string and returns the formatted string using a specified string format locale and arguments. It uses default locale by calling the Locale.getDefault() method if there is not a locale specified in the string.format() method. It specifies the locale to be applied on the format() method.

In other words, we can say that it replaces each format item in a specified string with the text equivalent of the corresponding value of an object. The argument that is passed is the argument referenced by the format specifiers in the format string. If there are more arguments than the format specifiers, the extra arguments are ignored. The number of arguments may be zero or more.

This method is like the sprintf() function in the C language and the println() method in the Java language.

Note: It throws illegalFormatException() if the format specifier is incompatible with the given string, insufficient arguments are provided by the format string, if the format string contains an illegal syntax or any other illegal conditions that may be a problem in formatting the string.

It also throws NullPointerException() if format is null.

It has the following two variants also which should be discussed.

  • Java String format(String format, object…args)
  • Java String format(Locale locale, String format, object…args)

Let's see an example of this method.

Example

The preceding example contains outputs for both of the variants.

  1. import java.util.Locale;  
  2.   
  3. public class FormatVariants   
  4. {  
  5.    public static void main(String args[])  
  6.    {  
  7.       String str1="Delhi";  
  8.       String str2="This is a %s test string";  
  9.       String str3="The value of 1+1 = %d";  
  10.       Double data=3145.6546;  
  11.       String strf1=String.format("Capital of India is %s", str1);  
  12.       String strf2=String.format("Given value is %f", data);  
  13.       String strf3=String.format("Now value is %21f", data);  
  14.       System.out.println("Output for 'format(String format, object…args)':");  
  15.       System.out.println(strf1);  
  16.       System.out.println(strf2);  
  17.       System.out.println(strf3);  
  18.       System.out.println(String.format(str2, "FORMAT"));  
  19.       System.out.println(String.format(str3, 2));  
  20.       System.out.println("Output for 'format(Locale locale, String format, object…args)':");  
  21.       System.out.println(String.format(Locale.UK, "Capital of India is %s",str1));  
  22.       System.out.println(String.format(Locale.UK, str2, "FORMAT"));  
  23.       System.out.println(String.format(Locale.UK, str3, 2));  
  24.    }  
  25. }  

Output


The next session is right here, click the link below.

Thank you, keep learning and sharing.


Similar Articles