Comparing Strings in Java – Complete Tutorial
|I have already discussed that how to initialise strings and what is the different ways of creating a String in java. Lets discuss what are the different ways of comparing Strings and their parts in Java.
So main methods that are used for String comparision are-
- boolean endsWith(String suffix) – This method checks whether a string starts with the specified sequence of characters or not.
- boolean startsWith(String prefix) – This method checks whether a string ends with the specified sequence of characters or not.
- boolean startsWith(String prefix, int offset) – It checks the String beginning at the offset and see whether the String starts with the string in the argument.
- int compareTo(String anotherString) – This method compares the two Strings and return an integer based on whether the String is greater(> 0), equals( = 0 ) or smaller( < 0) than the argument and the value will be equal to the difference between the ASCII values of first un-matching character.
- int compareToIgnoreCase(String anotherString) – This method is same as the above one except it ignores if there is only difference in the case of the string to be compared.
- boolean equals(Object anObject) – This method returns true if the String is exactly equal to the parameter passed otherwise returns false.
- boolean equalsIgnoreCase(Object anObject) – This is same as above method but it ignores if there is only difference in the case of the Strings.
- boolean matches(String regex) – This mathes the String based on the available Regex pattern and returns true if the String follows the Patters otherwise false.
All these methods are already self explanatory but lets see a common example which covers all of them.
package codingeekStringTutorials; public class CompareStringsExample { public static void main(String[] args) { String name = "CODINGEEK- A PROGRAMMING PORTAL"; String site = "codingeek- a programming portal"; String prefix = "CODINGEEK"; String suffix = "PORTAL"; System.out.println("Do name starts with defined prefix - " + name.startsWith(prefix)); System.out.println("Do name ends with defined suffix - " + name.endsWith(suffix)); //25 is the index of P of PORTAL System.out.println("Do name STARTS with defined SUFFIX at the end - " + name.startsWith(suffix, 25)); System.out.println("Is name and site are equal - " + name.equals(site)); System.out.println("Is name and site are equal(Ignoring Case) - " + name.equalsIgnoreCase(site)); System.out.println("Is name and site are comparable - " + name.compareTo(site)); System.out.println("Is name and site are comparable(Ignoring Case) - " + name.compareToIgnoreCase(site)); String email ="codingeek@gmail.com"; String regex = "^[_A-Za-z0-9-+]+(.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(.[A-Za-z0-9]+)*(.[A-Za-z]{2,})$"; System.out.println("does this email matches the Regex - "+ email.matches(regex)); } }
Output:-
Do name starts with defined prefix - true
Do name ends with defined suffix - true
Do name STARTS with defined SUFFIX at the end - true
Is name and site are equal - false
Is name and site are equal(Ignoring Case) - true
Is name and site are comparable - -32
Is name and site are comparable(Ignoring Case) - 0
does this email matches the Regex - true
If you have any doubt or need any other help.. Just comment below and I will try to solve it asap.
Good work….Now i got a complete idea about comparison of strings
Keep it up 😀
Thanks Rohit. Its great that you get a clear idea about this stuff.. We will keep providing such content in the future.. Keep reading..