How do I check a string ends with a specific word?

The String.endsWith() method can be used to check if a string ends with a specific word. It will return a boolean true if the suffix is found at the end of the string object.

In this example we will start the code by creating a class called StringEndsWithExample. This class has a standard main() method that makes the class executable. In the main() method we create a string variable called str and assign a text to it.

On the following line you can see an if conditional statement to check it the str string ends with "lazy dog". If it ends with that words then the corresponding block in the if statement will be executed.

package org.kodejava.lang;

public class StringEndsWithExample {
    public static void main(String[] args) {
        String str = "The quick brown fox jumps over the lazy dog";

        // well, does the fox jumps over a lazy dog?
        if (str.endsWith("lazy dog")) {
            System.out.println("The dog is a lazy dog");
        } else {
            System.out.println("Good dog!");
        }

        // Ends with empty string.
        if (str.endsWith("")) {
            System.out.println("true");
        }

        // Ends with the same string.
        if (str.endsWith(str)) {
            System.out.println("true");
        }
    }
}

Another thing that you need to know is that the endsWith() method will return true if you pass in an empty string or another string that is equals to this string as the argument. This method is also case-sensitive.

When you run the code snippet above you can see the following lines printed out:

The dog is a lazy dog
true
true

How do I know the length of a string?

To get the length of a String object we can use the String.length() method. This method returns the string length as an int value. The length of the string is equals to the length of the sequence of characters represented by this string object, and it is equals to the number of 16-bit Unicode characters in the string.

The following code snippet shows how to use the length() method. We create a string object name and assign a text to it. Call the method and store the length returned to the length variable and print it out. We can also use the length() method to check if the string is empty by checking if the length is equals to zero.

package org.kodejava.lang;

public class StringLengthExample {
    public static void main(String[] args) {
        String name = "Kode Java - Learn Java by Examples";

        // Get the length of the string specified by the name
        // variable.
        int length = name.length();
        System.out.println("Length = " + length);

        // Set name to an empty string and test if it is empty.
        name = "";
        length = name.length();
        if (length == 0) {
            System.out.println("The string is empty!");
        }
    }
}

If you run this code you will get the following output:

Length = 34
The string is empty!

How do I check string for equality?

To compare strings for their content equality we must use the String.equals() method. This method ensures that it will compare the content of both string instead of the object reference of the both string. This method returns true if both string in comparison have the same content.

Do not, never, use the == operator to compare string for its content. The == operator check for object reference equality, it returns true only if both objects point to the same reference. This operator returns false if the object doesn’t point to the same references.

package org.kodejava.lang;

public class StringEquals {
    public static void main(String[] args) {
        String s1 = "Hello World";
        String s2 = new String("Hello World");

        // This is good!
        if (s1.equals(s2)) {
            System.out.println("1. Both strings are equals.");
        } else {
            System.out.println("1. Both strings are not equals.");
        }

        // This is bad!
        if (s1 == s2) {
            System.out.println("2. Both strings are equals.");
        } else {
            System.out.println("2. Both strings are not equals.");
        }
    }
}

In the example above we deliberately create an instance of s2 string using the new operator to make sure that we have a different object reference. When you run the program it will give you the following result:

1. Both strings are equals.
2. Both strings are not equals.

How do I check if a string contains a specific word?

In this code example we are going to learn how to find a specific word or text inside a string. For this example we will utilize the java.lang.String class. The String class provides a method called String.indexOf(). It takes an argument of a String, which is the sub string that we want to find in the other string. You can imagine the sub string as a needle that we are going to find in the haystack.

If the word found more than once in the string, the indexOf() method returns the first index of a sub string found in the specified string. If the sub string can’t be found this method returns -1. The index of string returns by this method begin at zero. It means that the first letter in the string have the index number 0.

Another way that we can use is the String.contains() method. This method introduced in JDK 1.5. The method simply return a boolean value which is true or false indicating whether the string in search is found in the haystack. Let’s see the snippet below:

package org.kodejava.lang;

public class StringContainsExample {
    public static void main(String[] args) {
        String haystack = "Kodejava - Learn Java by Examples";

        // Checks to see if the word "Java" is found in the haystack
        // variable.
        String needle = "Java";
        if (haystack.indexOf(needle) != -1) {
            System.out.println("Found the word " + needle +
                    " at index number " + haystack.indexOf(needle));
        } else {
            System.out.println("Can't find " + needle);
        }

        // Or use the String.contains() method if you are not interested
        // with the index of the word.
        if (haystack.contains(needle)) {
            System.out.println("Eureka we've found Java!");
        }
    }
}

Running the example gave you the following result:

Found the word Java at index number 17
Eureka we've found Java!