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
- public class EqualsVariants
- {
- public static void main(String args[])
- {
- String str1="See you later";
- String str2="Come Back Soon";
- String str3="SEE YOU LATER";
- String str4="See you later";
- System.out.println("Outputs for equals()");
- System.out.println("Does str1 equals to str2..?: "+str1.equals(str2));
- System.out.println("Does str1 equals to str3..?: "+str1.equals(str3));
- System.out.println("Does str1 equals to str4..?: "+str1.equals(str4));
- System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.equals("Come Back Soon"));
- System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.equals("SEE YOU LATER"));
- System.out.println("Does str1 equals to 'See you later'..?: "+str1.equals("See you later"));
- System.out.println("Outputs for equalsIgnoreCase()");
- System.out.println("Does str1 equals to str2..?: "+str1.equalsIgnoreCase(str2));
- System.out.println("Does str1 equals to str3..?: "+str1.equalsIgnoreCase(str3));
- System.out.println("Does str1 equals to str4..?: "+str1.equalsIgnoreCase(str4));
- System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.equalsIgnoreCase("Come Back Soon"));
- System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.equalsIgnoreCase("SEE YOU LATER"));
- System.out.println("Does str1 equals to 'See you later'..?: "+str1.equalsIgnoreCase("See you later"));
-
- StringBuffer str5= new StringBuffer("See you later");
- StringBuffer str6= new StringBuffer("Come Back Soon");
- StringBuffer str7= new StringBuffer("SEE YOU LATER");
- System.out.println("Outputs for contentEquals()");
- System.out.println("Does str1 equals to str6..?: "+str1.contentEquals(str6));
- System.out.println("Does str1 equals to str7..?: "+str1.contentEquals(str7));
- System.out.println("Does str1 equals to str5..?: "+str1.contentEquals(str5));
- System.out.println("Does str1 equals to 'Come Back Soon'..?: "+str1.contentEquals("Come Back Soon"));
- System.out.println("Does str1 equals to 'SEE YOU LATER'..?: "+str1.contentEquals("SEE YOU LATER"));
- System.out.println("Does str1 equals to 'See you later'..?: "+str1.contentEquals("See you later"));
- }
- }
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.
- import java.util.Locale;
-
- public class FormatVariants
- {
- public static void main(String args[])
- {
- String str1="Delhi";
- String str2="This is a %s test string";
- String str3="The value of 1+1 = %d";
- Double data=3145.6546;
- String strf1=String.format("Capital of India is %s", str1);
- String strf2=String.format("Given value is %f", data);
- String strf3=String.format("Now value is %21f", data);
- System.out.println("Output for 'format(String format, object…args)':");
- System.out.println(strf1);
- System.out.println(strf2);
- System.out.println(strf3);
- System.out.println(String.format(str2, "FORMAT"));
- System.out.println(String.format(str3, 2));
- System.out.println("Output for 'format(Locale locale, String format, object…args)':");
- System.out.println(String.format(Locale.UK, "Capital of India is %s",str1));
- System.out.println(String.format(Locale.UK, str2, "FORMAT"));
- System.out.println(String.format(Locale.UK, str3, 2));
- }
- }
Output
The next session is right here, click the link below.
Thank you, keep learning and sharing.