Java String indexOf(int ch) Method

 Posted On  | Yashwant Chavan 

In this tutorial you will learn how to get index of particular character of the String. Java String.indexOf() method returns the index of character within this string.

Refer below steps

  • String.indexOf(int ch) method returns the index within this string of the first occurrence of the specified character.
  • Returns -1 if the character does not occur, Refer last scenario.

Java String indexOf(int ch) method example

package com.technicalkeeda.app;

public class StringIndexOf {

    public static void main(String args[]) {

        String str = "hello";

        System.out.println("Index of o:= " + str.indexOf('o'));
        System.out.println("Index of l:= " + str.indexOf('l'));
        System.out.println("Index of A:= " + str.indexOf('A'));

    }
}

Output

Index of o:= 4
Index of l:= 2
Index of A:= -1


© technicalkeeda.com 2017

 |  Find us on Google+ |  Rss Feed

Loaded in 0.0329 seconds.