How to find Index of string in Power Apps canvas apps

Hello everyone and welcome to my blog. In today’s blog I will discuss on how find index of specific phrase inside a string in canvas apps.

Canvas apps have the Index function which is frequently confused to work with strings. The Index function is used to find a specific item by index position from a table/ collection/ array. You can read more about that here.

Coming back to our topic, let’ start with an example. Below is a sample text I will use for this demo.

Microsoft have D365 Portals can be used to build customer facing websites. D365 Portals have templates you can get started with.

I want to find out the index of the word D365 in the above string. Power Apps does not have any inbuilt function like IndexOf which you usually find in other programming languages. Instead Power Apps have the Find function which give us similar functionality.

Below if the Find function in action.

As you can see from the above screenshot, the function returns 16 which the first occurrence of the word D365.

Well it’s good but it’s not the complete functionality. The first and foremost thing to remember is Find function is case sensitive. So the following construct does not give me any result.

For case insensitive comparison, you need to use the Upper or Lower function as shown in the below screenshot.

The second issue is the Find function only return the position of first occurrence. Very similar to firstIndexOf functions in other languages.

But how to get the index of last occurrence or index of all occurrences? Below is the code to accomplish the same.

Clear(indexes);
With(
    {
        substrings: Split(
            "Microsoft have D365 Portals can be used to build customer facing websites. D365 Portals have templates you can get started with.",
            "D365"
        )
    },
    ForAll(
        substrings,
        Collect(
            indexes,
            Sum(Last(indexes).Value, Len(ThisRecord.Result),4 /** the length of the phrase you want to find. Ours is D365. So it is 4 **/)
        )
    )
);

First I am using the Split function on the phrase to separate the whole string into substrings. Below is the portion of the code which is doing it. Check for the comment within /** <comment> **/ for better understanding.

substrings: Split(
            "Microsoft have D365 Portals can be used to build customer facing websites. D365 Portals have templates you can get started with.",
            "D365"
        )

The next part is fairly easy. I iterate through substrings and then store each occurrence inside a collection called indexes. Below is the code accomplish the same.

ForAll(
        substrings,
        Collect(
            indexes,
            Sum(Last(indexes).Value, Len(ThisRecord.Result),4 /** the length of the phrase you want to find. Ours is D365. So it is 4 **/)
        )
    )

All set. Now it’s time to show all the occurrences. In the below code I am retrieving the first and last index of the string.

"First Index: " & (Index(
    indexes,
    1
).Value + 1) - 4 /** Length og the search phrase **/  & Char(13) & "Last Index: " & (Index(
    indexes,
    CountRows(indexes) - 1
).Value + 1) - 4 /** 4 length of the search phrase. You will need to change it accordingly. **/

And that’s it. Our requirement is complete.

Hope you liked the post. If this post has helped, you can buy me a coffee. Links on right hand panel.

For more such interesting topics on Microsoft.NET and Power Platform, subscribe to my blog. Link to subscribe on the right pane.

You can also check out my other posts.

Debajit Dutta
Business Solutions MVP


Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe to get the latest posts sent to your email.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading