Java – String indexOf() Example, Strings indexOf() Program

String occurrence can be found by indexOf() method in java. indexOf() method returns value in integer. If string character is found in string, it will return index of first occurrence of string in positive value, otherwise return in negative value. indexOf() is searching method in string. Use of String indexOf() in java is shown below.

Example of String indexOf() in java, JSP

public class StringIndexOfExample {

    public static void main(String[] args) {

        String string = "find strings";

        // returns -ve if not found, +ve value if found successfully
        int indexAtStringCharacterStarts = string.indexOf("ind");

        int notFound=string.indexOf("java");

        System.out.println("String found At :"+indexAtStringCharacterStarts);

        System.out.println("String not found :"+notFound);
    }
}

Output

String found At :1
String not found :-1

Tags:

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post