Compare Strings in Java

Java's String class contains multiple methods for comparing full strings and portions of strings. Some of the methods return integer values, while others return boolean values. This guide will show you multiple ways to compare two different strings, using different methods already in the String class.

Steps

  1. Create two String objects. The simplest way to declare two strings in Java is to use the following code. "String" is the declaration of the object type. string1 and string2 are the names of each string. The words, "hello" and "father," are essentially the values of the string objects. Whenever your compiler encounters the quotations, it will automatically create a String.
  2. Use boolean startsWith(String prefix) to compare the start of strings. This String class method compares the start of a string with whatever you enter into the method. Since it is a boolean method, it will return either "true" or "false." Use the following code and one of your previously created strings. The following code will show you an example of a true case and a false case. It will also print out a message with the result. The output should be "true" and then "false."
  3. Use boolean startsWith(String prefix, int offset). This String class method will compare a string with whatever you enter in as the prefix at the point in the string specified by the value in offset. This method will ignore letters in the string that are before the value in offset. The following code will show you an example of a true case and a false case. Note that while counting letters in the string, start at 0. For example, in the word "hello," 'h' is space 0 and 'e' is space 1.
  4. Use boolean endsWith(String prefix) to compare the end of strings. This will compare the end of the string with whatever you enter into the method. Based on the value of the length of the string prefix, the method will count backward that value in the string to be compared. Look at the following code for an example of a true case and a false case. The code will also print out the result of the comparison.
  5. Use int compareTo(String anotherString) to compare two strings. Perform a comparison of the two strings by using string1.compareTo(string2) or string2.compareTo(string1). Since the method is of type integer, the method will return a number. If the strings are not equal to each other, the number returned will not be 0. It they are equal, the number returned will be 0. The following code will show you an example of an equal and non-equal case. The code will also print out the result to the system.

Tips

  • You can use this in a conditional statement in order to output an error message if the strings don't match:
  • If you simply want to check if two strings are equal, you might find it simpler to use the .equals() method:
    • NOTE: (myString1 == myString2) would actually return FALSE in this situation. Even though the strings "abc" are equal, myString1 and myString2 are two different instances of String. The "==" operator compares the objects, while the ".equals()" method compares the values of the strings.

Warnings

  • Do not try to compare strings using a simple == check unless you know what you are doing. This may not return the expected result and can significantly add to your debugging time, especially if you aren't an experienced Java programmer.
  • Comparing 2 null strings using the "==" operator returns TRUE, while comparing 2 null strings using .equals() throws an exception.

Related Articles

Sources and Citations