How to search a letter in a string?

How to search a letter in a string?

Previous Home Next

 

In this example particular string is searched in a given string .

Indexof method is used to search a word within a object.
 
To search a particular word in a given string use indexOf method.

 indexOf method. It returns a position index of a word within the string  if found. Otherwise it returns -1.


we can also search a word after particular position using

  indexOf(String word, int position) method.

 


public class SearchStringExample {
public static void main(String[] args) {

String strOrig = "Hello to world ";

int intIndex = strOrig.indexOf("to");

if(intIndex == - 1){

System.out.println("to not found");

}
else
{

System.out.println("Found to at index " + intIndex);
}
}


Found to at index 6
Previous Home Next