Replace all instances of text , case insensitive, in Power Apps canvas apps

Hello everyone and welcome to my blog. In today’s blog I will discuss how to replace all instances of word inside a string. I will demonstrate for both case sensitive and case insensitive scenarios.

Power Apps has the Substitute function to replace all instances of word inside a text. For this blog, I will use the following text.

“The quick brown fox jumps over the Lazy dog. Dog is preferred over FOX as pets”

I want to replace all instances of the word – fox with the word dog. First I will start with the out of the box Substitute function. Below is the Substitute function in action.

Observe the output. While the first instance of the word – fox is replaced by the word dog, the second instance – FOX is not replaced. The reason for that being Substitute function is case sensitive. Hence the word FOX was not replaced.

How to do it then? Well, if you need the case insensitive replacement, you need to use the MatchAll function where you can specify regular expressions. MatchAll function support case insensitive match as well.

The solution however is not straightforward. You need to use MatchAll in combination with Substitute function to achieve the result. So let’s see how we can do this.

For the text conversion, I have introduced a button control. When the button is clicked, the word Fox shall be replaced with dog and the operation shall be case insensitive.

The initial text is displayed inside a label control and is stored in a variable labelText which is set to the Text property property of the label. Check for the screenshots below.

When the button is clicked, we will perform the replacement operation. Below is the formula on On Select of the button.

ClearCollect(labelTextColl, labelText);

With(
    {
        matches: MatchAll(
            labelText,
            "fox",
            IgnoreCase
        )
    },
    ForAll(
        matches,
        Collect(labelTextColl, Substitute(
            Last(labelTextColl).Value,
            ThisRecord.FullMatch,
            "dog"
        ))
    )
);

UpdateContext({labelText: Last(labelTextColl).Value});

As I told you it’s not that simple. And that’s how Power Apps is. Sometimes complex integrations you can set-up in minutes and then you have these basic functionalities for which you need to literally struggle.

Let’s dissect the formula. Since UpdateContext function cannot be used inside ForAll statement in Power Apps, I am using a Collection variable labelTextColl. If you want to know more on this limitation and it’s work around, you can read my blog here.

After I declare the collection, the next step is to find all possible matches for the word fox irrespective of casing. For that I use the following formula.

matches: MatchAll(
            labelText,
            "fox",
            IgnoreCase
        )

Observe I am using the MatchAll function with IgnoreCase option. The function returns all the matches for the word fox irrespective of casing. The possible matches returned is stored within an scope variable matches in the With function.

The next step is to iterate through all matches of the word fox and replace it with the word dog. For that I used the below formula.

 ForAll(
        matches,
        Collect(labelTextColl, Substitute(
            Last(labelTextColl).Value,
            ThisRecord.FullMatch,
            "dog"
        ))
    )

The code is pretty simple and obvious. Every time I replace a matching word, I store the output inside the collection declared in earlier step and then reuse this output for the next match. And that’s why the Substitute function always uses the last item of the collection as it’s input.

The final step is to update the variable labelText once the entire operation is completed. For this, we again use the UpdateContext function to update the value of the variable.

UpdateContext({labelText: Last(labelTextColl).Value});

And below is the behavior in action.

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

You will also like the below 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