Java String indexOf() Method with example | java substring indexof
JavaStringindexOf()methodisusedtofindtheindexofaspecifiedcharacterorasubstringinagivenString.Thereare4variationsofthismethodinStringclass[1]:TheindexOf()methodsignatureintindexOf(intch):ItreturnstheindexofthefirstoccurrenceofcharacterchinagivenString.intindexOf(intch,intfromIndex):Itreturnstheindexoffirstoccurrenceofcharacterchinthegivenstringafterthespecifiedindex“fromIndex”.Forexample,iftheindexOf()methodiscalledlikethisstr.indexOf(‘A’,20)thenitwouldstartlookingforthecharacter‘A’instringst...
Java String indexOf() method is used to find the index of a specified character or a substring in a given String. There are 4 variations of this method in String class[1]:
The indexOf() method signatureint indexOf(int ch): It returns the index of the first occurrence of character ch in a given String.
int indexOf(int ch, int fromIndex): It returns the index of first occurrence of character ch in the given string after the specified index “fromIndex”. For example, if the indexOf() method is called like this str.indexOf(‘A’, 20) then it would start looking for the character ‘A’ in string str after the index 20.
int indexOf(String str): Returns the index of string str in a particular String.
int indexOf(String str, int fromIndex): Returns the index of string str in the given string after the specified index “fromIndex”.
All the above variations returns -1 if the specified char/substring is not found in the particular String.
Java String indexOf()...