String Manipulation: finding the position of n-th occurence of character


I’m looking for a function that i’d hoped to find in the String Manipulation, that outputs the position/index of the n-th occurence of a given string like

nthIndexOfChar(str, toSearch, n)

nthIndexOfChar(“abcabc”, “a”, 2) = 4

Any help is appreciated!

Thanks in advance,

If you don’t mind using a Java Snippet node, here’s a solution…
In the Java Snippet node, go to the “Additional Bundles” tab and add the bundle “org.apache.commons.lang3 3.3.1”.

In the “Java Snippet” tab add the custom import “import org.apache.commons.lang3.StringUtils;”
Add an integer column (give it name like indexofnthoccurrence) using the Add button at the bottom of the window. A variable for the column will automatically be inserted in the snippet like "out_indexofnthoccurrence = ".
Assign an index to this variable using this one-liner:
out_indexofnthoccurrence = StringUtils.ordinalIndexOf(c_stringTest, “cat”, 2);
Here we’re looking for the second occurrence of “cat” in the cell of the stringTest column.
(Substitute the column of your choice by removing c_stringTest and double-clicking the col of your choice in the columns list on the left of the dialog.) Likewise, you can substitute a variable for the search string.
The config dialog should look similar to this:

Hope this helps
String nth occurrence.knwf (6.0 KB)


Hi Don,

thanks, did exactly what i wanted!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.