string.indexOf() Returns the position of the matching string or char from left.
input : Can be char or string.
startPos : (optional ) Int position from where the matching will start

Java String indexOf()

Note these points
Matching position is always returned from left, first position is 0 ( zero ).
First occurrence is returned ( from startPos index if given ) .
Search starts from 0 position if startPos is not given.

Using char input
String my_str="Welcome to";
System.out.println(my_str.indexOf("2"));// 15
System.out.println(my_str.indexOf("e"));// 1
Using char and startPos
String my_str="Welcome to";
System.out.println(my_str.indexOf("e",1));// 1 
System.out.println(my_str.indexOf("e",2));// 6
Using string input
String my_str="Welcome to";
System.out.println(my_str.indexOf("co"));// 3
Using string and startPos
String my_str="Welcome to";
System.out.println(my_str.indexOf("co",3));// 3
System.out.println(my_str.indexOf("co",4));// 20
System.out.println(my_str.indexOf("com",4));// 20
Getting position of @ and . inside a email address.
System.out.println("".indexOf("@"));// 6
System.out.println("".indexOf("."));// 14

Difference between indexOf() and lastIndexOf()

These pictures shows the difference between indexOf() and lastIndexOf()

Java String indexOf()

Java String lastIndexOf()

All String functions


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer