java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to check a string starts with a specified word?

public class StartsWithSample
{
    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");
        }
    }
}