Java Check a Word in Sentence

For detect a sentence contains a specific word, you can use indexOf function like example code below:
package myjavaexample; 
 
public class MyJavaExample 
{ 
    public static void main(String[] args) { 
        String sentence="I like Java programming language, because its can be use in Android also"; 
        String word = "Java"; 
        if (sentence.indexOf(word) != -1) { 
            System.out.println("Found the word \"" + word+"\" at index number " + sentence.indexOf(word)); 
        } else { 
            System.out.println("Can't find \"" + word+"\" inside \""+sentence+"\""); 
        } 
    } 
} 

Comments



Popular posts from this blog

Simple Java Code HTML Highlighter

How to Use Extends and Implements

Fibonacci Algorithm in Java Array