String class have numbers of methods for comparing strings and portions of strings. Strings can be compare on the basis of content and their references. Below some string class methods which are used to compare strings are :
Modifier and Method Name
|
Description
|
boolean endsWith(String suffix)
boolean startsWith(String prefix)
|
Returns true if this string ends with or begins with the substring
specified as an argument to the method.
|
boolean startsWith(String prefix, int offset)
|
Considers the string beginning at the index offset, and returns true
if it begins with the substring specified as an argument.
|
int compareTo(String anotherString)
|
Compares two strings lexicographically. Returns an integer indicating
whether this string is greater than (result is > 0), equal to (result is =
0), or less than (result is < 0) the argument.
|
int compareToIgnoreCase(String str)
|
Compares two strings lexicographically, ignoring differences in case.
Returns an integer indicating whether this string is greater than (result is
> 0), equal to (result is = 0), or less than (result is < 0) the
argument.
|
boolean equals(Object anObject)
|
Returns true if and only if the argument is a String object that
represents the same sequence of characters as this object.
|
boolean equalsIgnoreCase(String anotherString)
|
Returns true if and only if the argument is a String object that
represents the same sequence of characters as this object, ignoring
differences in case.
|
boolean matches(String regex)
|
Tests whether this string matches the specified regular expression.
|
1) String Comparison using equals() method
this method compares two strings content
String s1 = "pushkar"; String s2 = "pushkar"; System.out.println(s1.equals(s2));
It will print true because both strings s1 and s2 have same content.
2) String Comparison using equalsIgnoreCase() method
this method compares two strings content weather, it's in uppercase or lowercase, but both strings contains same sequence of characters .
String s1 = "pushkar"; String s2 = "PUSHKAR"; System.out.println(s1.equalsIgnoreCase(s2));
It will print true because this method will ignore case.
3) String Comparison using == operator
The == operator is used to compare two strings references not their content. Here references means their memory addresses, weather they belongs to same memory or not. Because Java uses two memory locations to store Strings i.e all the String literals are store in StringConstantPool and Strings created with new operator are store in Heap memory.
String s1 = "pushkar"; String s2 = "pushkar"; System.out.println(s1 == s2); // Line 1 String s3 = new String("pushkar"); System.out.println(s2 == s3); // Line 2
Line 2 will print false because both have different memory references.
4) String Comparison using compareTo() method
This method return integer values i.e. 0,1 and -1. Suppose we have two strings s1 and s2 and if s1 == s2 it will return 0, if s1 > s2 it will return 1 and if s1 < s2 it will return -1.
String s1 = "pushkar"; String s2 = "pushkar"; String s3 = "pushkara"; System.out.println(s1.compareTo(s2)); //it will print 0 System.out.println(s3.compareTo(s1)); //it will print 1 System.out.println(s1.compareTo(s3)); //it will print -1
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.


No comments:
Post a Comment