The Power Automate LastIndexOf function returns the index of the last occurrence of a substring, starting from 0, with a case-insensitive search.
In this article, I’ll introduce you to the lastIndexOf function and will provide a step-by-step guide on how to use the function in a Microsoft flow.
Power Automate LastIndexOf Function
The function returns the index value of the last occurrence of a substring, starting from 0. The search is not case-sensitive.
Syntax
lastIndexOf('<text>', '<searchText>')
Input parameters
- text (mandatory): The string within the search string should be found.
- searchText (mandatory): The search string that should be found in the given text.
Return value
- If the searched string is found: Returns the index value for the last occurrence of a given substring, starting from its initial position.
- If the searched string is NOT found: Returns -1.
- If the searched string and text are empty: Returns 0.
Power Automate LastIndexOf Expression Example
Let’s take a look at a lastIndexOf usage example. We want to find out, at which index the ‘you’ within ‘Where are you?’ starts.
The example lastIndexOf expression would look like this:
lastIndexOf('Where are you?','You')
=> 10
To get a better understanding, why the result is ten, take a look at the following table. The first line is every character in the text. The second line is the index for each character starting at 0. You see that the ‘y’ of ‘you’ starts at 10.
W | h | e | r | e | a | r | e | y | o | u | ? | ||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
Since you only appears once within the text, it is not a good example for the lastIndexOf function, since indexOf function would work here too.
The following example describes the difference between both functions.
lastIndexOf('abc abc','abc')
=> 4
indexOf('abc abc','abc')
=> 0
a | b | c | a | b | c | |
0 | 1 | 2 | 3 | 4 | 5 | 6 |
How To Use The Power Automate LastIndexOf Function
Follow the steps to use the Power Automate LastIndexOf function in a flow.
-
Create a new flow with trigger ‘Manually trigger a flow’
New flow
-
Add a new step by clicking on ‘+ New step’
Add a new step
-
Search for ‘initialize’ and click on ‘Initialize variable’
Add Initialize variable action
-
Setup Initialize variable: 1) Provide a name, 2) select type String, 3) Set value to ‘Where are you?’
Setup variable
-
Add a compose step by searching for ‘compose’ and clicking on ‘Compose’
Add Compose action
-
Setup compose action: 1) click into Inputs (popup appears), 2) click on ‘Expression’, 3) set expression to ‘lastIndexOf(variables(‘text’),’You’)’ 4) click on ‘OK’
Setup lastIndexOf expression in Power Automate
-
Save and do a test run
Result test run – Power Automate lastIndexOf function example