Wednesday 6 February 2019

String Operations

String Operations

How to find nth occurrence of character in a string?

Nowadays there IS support of Apache Commons Lang’s StringUtils,

This is the primitive:

int  index = StringUtils.ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal)

for a given string like:

String exampleString = " This is substring example, and is an effective one". 

to find second index of is the implementation will look like

StringUtils.ordinalIndexOf(exampleString, "is", 2)

You can also find the last nth occurrence of a character in a string with the lastOrdinalIndexOf method.