To check the position of a string or a char within another string you use the Power Automate indexOf function or the Power Automate Find text position. Both work the same.
The difference between the indexOf function and the Find text position action is, that the function is used within expressions and the action is a step within a Microsoft flow.
In addition to the indexOf function there are two variants of indexOf: the lastIndexOf function and the nthIndexOf function.
Within this article you will get to know and understand all functions and the action. For each one I’ll explain the details and then provide a lot of examples.
Before we dive into the details, you might ask yourself, why I need to find out the position of a string within another one at all?
I’m pretty sure the main use case for finding the index of a string within a string is the need to manipulate the string. Think for example of an email address which contains of the name and the domain separated by the @ symbol.
In case you need to extract one of these for instance with the substring function, you need to know where your substring starts or ends. This is where indexOf is the solution.
But probably you here because you already have the need for the indexOf function. So let’s start!
Power Automate IndexOf Function
The Power Automate indexOf function searches a given string within another string. If a match is found, the first match position is returned by starting to count by zero from the start of the text.
Note: The Power Automate indexOf function is NOT case-sensitive. (indexOf(‘ab’,’a’) and indexOf(‘ab’,’A’) return both zero)
- text (mandatory):
- searchText (mandatory):
- a number greater or equal to zero, the position where the searchText was found for the first time in the text. Starting at 0 for the first position.
- -1, if the searchText is not found
Example Power Automate IndexOf Expression
indexOf('abc','ab') => 0 indexOf('abc','x') => -1 indexOf('abcabc','b') => 1
Power Automate LastIndexOf Function
The Power Automate lastIndexOf function has the same parameters as the indexOf function. There is just one difference in the behavior of the function.
As the name already implies: lastIndexOf returns the position of the last found match within a string. IndexOf returns the first match.
But let’s show some examples, to clarify what is the difference.
Power Automate lastIndexOf Examples
lastIndexOf('TNT','T') => 2 indexOf('TNT','T') => 0
Power Automate NthIndexOf Function
We now know how to find the first and the last appearance of a searched string. But what about matches between the firsat and the last match?
This is where you can use the Power Automate nthIndexOf function. This function lets you define the index of which search match do you need.
nthIndexOf('abcabcabc','a',1) => 0 nthIndexOf('abcabcabc','a',2) => 3 nthIndexOf('abcabcabc','a',3) => 6 nthIndexOf('abcabcabc','a',4) => -1
How To Use The Power Automate Find Text Position Action
Here is a simple Power Automate Find Text Position example. We will search for the @ symbol within an email. I’ll guide you how to setup everything.
Create a new ‘Manually trigger a flow’: Provide a name, select ‘Manually trigger a flow’ and click on ‘Create’
Create a new flow
Add a step to the flow by searching for ‘find text position’ and clicking on the Find text position action.
Adding Power Automate Find text position action
Set text to ‘[email protected]’ and Search text to ‘@’ in the Power Automate Find text position action
Power Automate Find text position action configuration
Do a test run of the flow
Result of test run @-smybol add position 4 found (Remember, index starts at zero!)
Power Automate IndexOf Function Examples
There are some situations where it might be not obvious how to use the Power Automate indexOf function. These parts are explained here. Let me know, if I missed some and I’ll try to add them.
To search for the first whitespace in a string just use the following indexOf expression.
indexOf('John Doe',' ') => 4
IndexOf new line
To search for the first new line in a string, you need to search for a new line. But how do you formulate a new line?
You can use the decodeUriComponent function to decode it:
So to search for the new line, you need to do the following:
To search for the first single quote within a Power Automate indexOf expression, you need to decode the single code like this:
Within the indexOf expression it would be like this: