The “indexOf” function is essential for data parsing. You’ll rarely parse some text and don’t use this function. It returns the first occurrence of a string inside another string, but if you want to get the last you can also use the lastIndexOf function. It will always return a number, and it will return:
- Zero or greater for the position of the search text
- -1 if the string is not found
Please note that the position starts at zero, not one. This is quite common for those who are programmers but can be confusing for those who are not.
Usage
It follows a simple pattern.
- Text
- String to search
Let’s see some examples. First, let’s set up our test environment. We’ll create an instant Flow with 2 parameters. This way, we can trigger the Flow with the parameters without changing it.
The formula will be as follows:
indexOf(triggerBody()['text'],triggerBody()['text_1'])
Example:
Let’s start with a simple example:
String 'Manuel T. Gomes'
Search 'T.'
will return
7
Notice that you can search for one or multiple characters. The search is case insensitive, meaning that “gOmeS” or “GoMES” are the same.
String ‘Manuel T. Gomes’
String 'Manuel T. Gomes'
Search 'Teixeira'
will return
-1
The function can’t find the string, so that it will return -1.
Limitations
I could not find a hard limitation for this function. I’ve passed strings with 30000 characters, and it returned almost instantly. But keep in mind that an undocumented limit may exist, so keep your strings as short as possible.
Recommendations:
- Keep the parameters in variables. Don’t pass them directly in the formula. You can do that, but it will make your formula quite prone to error. For example, if you want to check the string “Manuel ’T. Gomes’” (Notice the quotes), the function will return an error because it will consider the quotes as the string’s end. Having data on separate variables helps a lot in avoiding these issues.
- Don’t nest it. There’s no real reason to do it, but if you find yourself in a situation where you have nested indeO’ functions in a formula, you should review it ’because ‘you’re probably doing something wrong.
- Please note that formulas may have localization-based differences. For example, you should write “indexOf” with “separating each of the arguments, but if your regional settings are set to Portugal, you should use “;” instead.
Sources:
Microsoft’s indexOf Function Reference
Back to the Power Automate Function Reference.
Photo by Markus Winkler on Unsplash