Monday 14, Apr 2025
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

Related Posts:

  • How to replace the element from List Following example shows how to replace all the occurrence of element with another element using Collections.replaceAll(list, oldVal, newVal) method in Java. <String> boolean java.util.Collections.replaceAll(List<St… Read More
  • How to get Character unicode point in a StringFollowing example shows how to get unicode point of a character in a String using str.codePointAt(int position) method. Sample Programpackage com.javatutorialcorner.javastring;public class StringUnicode { public static voi… Read More
  • How to shuffle the element of collection (List) Following example shows how to shuffle the element of Collection (List) using Collections.shuffle(list); method in Java. void java.util.Collections.shuffle(List<?> list)shufflepublic static void shuffle(List<?> … Read More
  • String Concatenation in JavaFollowing example shows String concatenation in Java using + operator and StringBuffer.append() method. Sample Programpackage com.javatutorialcorner.javastring;public class StringConcatenation { public static void main(Str… Read More
  • How to Create File using Java Following example shows hoe to create file using File() constructor and file.createNewFile() method in Java createNewFile public boolean createNewFile() throws Atomically creates a new, empty file named by this abstract pa… Read More
  • 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