Java String indexOf() Method | int indexof java
❮StringMethods[1]ExampleSearchastringforthefirstoccurrenceof"planet":StringmyStr="Helloplanetearth,youareagreatplanet.";System.out.println(myStr.indexOf("planet"));TryitYourself»[2]DefinitionandUsageTheindexOf()methodreturnsthepositionofthefirstoccurrenceofspecifiedcharacter(s)inastring.Tip:UsethelastIndexOf[3]methodtoreturnthepositionofthelastoccurrenceofspecifiedcharacter(s)inastring.SyntaxThereare4indexOf()methods:publicintindexOf(Stringstr)publicintindexOf(Stringstr,intfromIndex)publicin...
❮ String Methods[1]
ExampleSearch a string for the first occurrence of "planet":
String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.indexOf("planet"));Try it Yourself »[2]
Definition and UsageThe indexOf() method returns the position of the first occurrence of specified character(s) in a string.
Tip: Use the lastIndexOf[3] method to return the position of the last occurrence of specified character(s) in a string.
SyntaxThere are 4 indexOf() methods:
public int indexOf(String str) public int indexOf(String str, int fromIndex) public int indexOf(int char) public int indexOf(int char, int fromIndex) Parameter Values Parameter Description str A String value, representing the string to search for fromIndex An int value, representing the index position to start the search from char An int value, representing a single character, e.g A, or a Unico...