Java String Comparison

Java String Comparison

Java String Comparison

String class in Java gives various techniques to look at strings or substrings inside strings.

In this instructional exercise, I will talk about those Java string comparison strategies.

equivalents() and equalsIgnoreCase()

We can utilize rises to() technique to check the balance of two strings. It has the following grammar.

Syntax:

boolean equals(String str)

Here str is the String object and equivalents() technique is conjured by another String object.

It returns genuine if both the strings are actually same generally returns bogus. The comparison is case touchy.

On the off chance that we need to think about strings disregarding case affectability, at that point we can utilize equalsIgnoreCase() technique.

It has the following language structure.

Example:

An example is given underneath that tells the best way to utilize equivalents() and equalsIgnoreCase() techniques in Java.

class temp
{
public static void main(String...s)
{
String str1="Hello";
String str2="HELLO";
System.out.println(str1+" equals "+str2+" -> "+str1.equals(str2));
System.out.println(str1+" equalsIgnoreCase "+str2+" -> "+str1.equalsIgnoreCase(str2));
} 
}

Output

Hello equals HELLO -> false
Hello equlsIgnoreCase HELLO -> true

regionMatches()

This technique is utilized to look at a particular locale of first string with the particular district of the second string.

regionMatches() has the following sentence structure.

Syntax:

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

Here str1StartIndex is the file at which area starts in the first string while str2StartIndex is the

list at which district starts in the second string. str2 is the subsequent string and the technique is summoned by a first string.

The length of the area that will be looked at is determined by numChars.

On the off chance that we need to think about explicit areas in two strings overlooking th

instance of characters then we utilize over-burden rendition of regionMatches() technique. It has the following language structure.

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

On the off chance that ignoreCase is valid, at that point instance of characters not considered and on the off chance that bogus, at that point instance of characters is considered

Example

Give us a chance to take one example to comprehend this technique.

class temp
{
public static void main(String...s)
{
String str1="Hello World";
String str2="HELLO WORLD";
System.out.println("Normal Version -> "+str1.regionMatches(0,str2,0,5));
System.out.println("Overloaded Version -> "+str1.regionMatches(true,0,str2,0,5));
}
}

Output:

Normal Version -> false
Overloaded Version -> true

startsWith() and endsWith()

startsWith() technique checks if a given string starts with the predetermined

string and endsWith() strategy checks if a given string closes with the predefined string. These two strategies

are the specific type of regionMatches(). They have the following sentence structure.

This technique checks whether a string is not exactly, more prominent than or equivalent to another string.

compareTo() is mostly utilized when we need to sort such a significant number of the string in word reference request.

It has the following sentence structure.

boolean statrsWith(String str)
 boolean endsWith(String str)

Example:

class temp
{
public static void main(String...s)
{
String str="BarBar";
System.out.println(str.startsWith("Bar"));
System.out.println(str.endsWith("Bar"));
}
}

Output:

true
true

This strategy checks whether a string is not exactly, more prominent than or equivalent to another string. compareTo() is essentially utilized when we need to sort such huge numbers of string in lexicon request. It has the following linguistic structure.

Under zero – The conjuring string is not exactly str.

More noteworthy than zero – The conjuring string is more noteworthy than str.

Zero – The two strings are equivalent.

Example:

class temp
{
public static void main(String...s)
{
String str1="Bar", str2="Bat",str3="Bag",str4="Bar";
System.out.println(str1+" and "+str2+" -> "+str1.compareTo(str2));
System.out.println(str1+" and "+str3+" -> "+str1.compareTo(str3));
System.out.println(str1+" and "+str4+" -> "+str1.compareTo(str4));
}
}

Output:

Bar and Bat -> -2
Bar and Bag -> 11
Bar and Bar -> 0

The worth that compareTo() returns is the distinction between the ASCII estimations of first inconsistent characters in quite a while.

In the above example when we looked at “Bar” and “Bat” at that point – 2 is returned.

Since in the two strings the main inconsistent character is ‘r’ and ‘t’, there ASCII esteems distinction is – 2.

The compareTo() strategy consider case contrast while looking at the strings.

In the event that you need to disregard case contrast, you can utilize compareToIgnoreCase() technique.

It has the following linguistic structure.

int CompareToIgnoreCase(String str)

This was the point by point instructional exercise about Java string comparison.

On the off chance that you have any questions in regards to above instructional exercise, at that point

don’t hesitate to ask by remarking beneath.

Recent Posts

error: Alert: Content is protected!!