java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to check if a string contains a specific word?

package com.javacoderanch.example.lang;

public class StringContains
{
    public static void main(String[] args)
    {
        String name = "Code Java";

        //
        // The indexOf() method return the first index of the substring found in
        // the specified string. If the substring is not found this method will
        // return -1
        //
        if (name.indexOf("Java") != -1)
        {
            System.out.println("Bravo Java!");
        }
        else
        {
            System.out.println("Can't find Java");
        }
    }
}