After working on a PowerApps project recently I needed to manipulate a few strings, replace characters, concatenate them, that kind of thing. I’ve summarised how to do a few of those in this post.
How to replace a substring in a string
In order to replace a string in Powerapps use the Substitute method, and specify which substring you want to replace, and what string to replace it with. For example:
//Set the main string Set(mainString,"Fill my glass with water."); //Change character(s), with another Set(mainString, Substitute(mainString,"water","beer"));
If you put a label on your screen so that it shows the variable mainString then you’ll see that after running that code mainString is “Fill my glass with beer.” because the word water has been replaced with the word beer in the original sentence.
How to replace a character in a string in Powerapps
Another way you might want to use this is to substitute a comma with a semi colon in your PowerApps string for instance, or a full stop (period) for an exclamation mark, in which case it would look like this:
//Set the main string Set(mainString,"Fill my glass with beer."); //Change character(s), with another Set(mainString, Substitute(mainString,".","!"));
… and after running that code mainString is “Fill my glass with beer!“
How to replace a line ending in a string
I had to read data in from a Sharepoint list and some of the longer text fields contained a line ending which caused some issues in the Powerapps project. In this case it worked to replace the line ending (char(10)) with a comma, like this:
How to concatenate strings
To join multiple strings in PowerApps use the Concatenate function like this:
//Create a string and save it in a variable Set(firstString,"This is the complete string"); //Create a second string and save it in a variable Set(secondString," in one sentence."); //Prepend first string to second string Set(finalstring,Concatenate(firstString,secondString));
If you now write finalString to a label you will see this sentence – “This is the complete string in one sentence.”
Note – I will be adding more useful PowerApps string examples here as they come up. You may also want to read my post about getting started with PowerApps.