How do I compare string regardless their case?

Here is an example of comparing two string for equality without considering their case sensitivity. To do this we can use equalsIgnoreCase() method of the String class. Let’s see an example below:

package org.kodejava.example.basic;

public class EqualsIgnoreCase {
    public static void main(String[] args) {
        String uppercase = "ABCDEFGHI";
        String mixed = "aBCdEFghI";

        // To compare two string equality regarding it case use the
        // String.equalsIgnoreCase method.
        if (uppercase.equalsIgnoreCase(mixed)) {
            System.out.println("Uppercase and Mixed equals.");
        }
    }
}

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

To test if a string starts with a specific word we can use the String.startsWith() method. This method returns a boolean true as the result if the string starts with that specific word.

package org.kodejava.example.lang;

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

        // See if the fox is a quick fox.
        if (str.startsWith("The quick")) {
            System.out.println("Yes, the fox is the quick one");
        } else {
            System.out.println("The fox is a slow fox");
        }
    }
}

The code snippet print the following output:

Yes, the fox is the quick one