How to search for characters from strings in JAVA ?

By | June 25, 2011

Hi,

For searching for a particular character from a string and getting the index of that particular character, use the following code.


public class MainClass{

  public static void main(String[] arg){

    String myStr = "Coderz";

    int strIndex = 0;
    strIndex = myStr.indexOf('o');

    System.out.println(strIndex);
  }

}

It will print out the index of ‘o’, that’s 1.

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *