string.indexOf()

string.indexOf() Returns the position of the matching string or char from left.
Syntax
string.indexOf(input,startPos)
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 plus2net.com";
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 plus2net.com";
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 plus2net.com";
System.out.println(my_str.indexOf("co"));// 3
Using string and startPos
String my_str="Welcome to plus2net.com";
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("userid@example.com".indexOf("@"));// 6
System.out.println("userid@example.com".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


plus2net.com



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-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer