We are given a string, say, "itiswhatitis"
and a substring, say, "is"
.
I need to find the index of 'i'
when the string "is"
occurs a second time in the original string.
String.indexOf("is")
will return 2 in this case. I want the output to be 10 in this case.
You can write a function to return array of occurrence positions, Java has String.regionMatches function which is quite handy
I am using: Apache Commons Lang: StringUtils.ordinalIndexOf()
i think a loop can be used.
This overload starts looking for the substring from the given index.
Use overloaded version of
indexOf()
, which takes the starting index (fromIndex) as 2nd parameter: