We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 3 May 2015

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.

Sample Program

package com.javatutorialcorner.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 {
System.out
.println("Word " + subString + " found at index " + index);
}

}

}


Output


The above code will produce the following result.



Word Java found at index 19

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to search a word inside String Rating: 5 Reviewed By: eHowToNow