How to search a word inside String

Following program shows how to search the word inside a string using indexOf(String str) available in Java. The indexOf(String str) method returns the position of word with in the string if found, otherwise it returns –1.

package com.ehowtonow.javastring;

public class WordSearch {

    public static void main(String[] args) {
        String jtc = "Spring tutorials @ Java Tutorials Corner";
        String subString = "Java";
        int index = jtc.indexOf(subString);
        if (index == -1) {
            System.out.println(subString + " not found");
        } else {
                    .println("Word " + subString + " found at index " + index);




Word Java found at index 19

Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. How to convert a String to Lower case
  2. How to search a word inside String
  3. Java String codePointBefore() method with example
  4. Java String codePointAt() method with example
  5. Java String contentEquals(StringBuffer sb) method with example
  6. Java String codePointCount() method with example
  7. How to find last occurrence of substring inside string
  8. How to Split the String in Java
  9. Java String contains() method with example
  10. Java String equals() method with example
  11. How to get Character unicode point in a String
  12. Java String indexOf(String str, int fromIndex) method with example
  13. Java String endsWith() method with example
  14. Java String indexOf(int ch, int fromIndex) method with example
  15. Java String indexOf(int ch) method with example

Be the first to comment

Leave a Reply