How can I search a character or substring in java?


You can search for a particular letter in a string using the indexOf() method of the String class. This method which returns a position index of a word within the string if found. Otherwise it returns -1.

Example

 Live Demo

public class Test {
   public static void main(String args[]){
      String str = new String("hi welcome to Tutorialspoint");
      int index = str.indexOf('w');
      System.out.println("Index of the letter w :: "+index);
   }
}

Output

Index of the letter w :: 3

Updated on: 30-Jul-2019

98 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements