Welcome to all my blog readers. Today’s topic is rather very simple. And especially so when Power Apps has been around for quite a few years now.
Today we are going to do understand how to work with strings in canvas apps and what are the functions available in canvas apps for the same.
If you want to perform substring operations in Power Apps, you can follow my blog here.
If you are from traditional programming background you must have heard of substring, indexOf, lastIndexOf functions. While the casing and syntax may differ they kind of do the same thing.
Unfortunately, there are no similar functions in Power Apps canvas apps. PowerApps is based on excel formulas and if you are accustomed to string manipulations in excel, you will find it quite similar in Power Apps.
I will use a rather complex string for this demo so that you get a fair idea of the Power Apps functions for string manipulations.
Below is the string I shall use.
<div class="ExternalClassFDD2F2576C604F0CA2921A1EE3758F61_842910862110408f98d4ea18dbd9bf4e"><div style="font-family: "calibri" , "arial" , "helvetica" , sans-serif ; font-size: 11pt ; color: rgb(0 , 0 , 0)"><a href="https://sampledemo.com#/site/Reports/views/Issue_tracking/IssueSummary" title="View"><span style="color: black"></span>View</a><br></div></div>
This is my raw string. And say I want to take out the value of the href property of the anchor tag (highlighted in bold) – https://sampledemo.com#/site/Reports/views/Issue_tracking/IssueSummary
For this demo, I have a canvas app with two label controls. One for the raw HTML and the other to display the URL value only.
Let’s see the formula to achieve this. This can be achieved in multiple ways but you need to use the similar set of formula that I have used nonetheless.
With(
{
urlRightChars: Right(
lblRawText.Text,
Len(lblRawText.Text) - Find(
"<a href=",
lblRawText.Text
) - 8
)
},
With(
{
replacePosition: Find(
$"{Char(34)}",
urlRightChars
) - 1
},
Left(
urlRightChars,
replacePosition
)
)
)
To be honest, it looks pretty daunting to start with. But as I explain you will find it much easier.
I have used the With function of Power Apps but it’s not required. But more often than not you will end up using it because of the value it brings in. So let’s start exploring.
Let’s begin with the first With function.
- Get the index of the starting position of the phrase – <a href=”. For this we have used the Find function of PowerApps. Find function search for the occurrence of a specified phrase in a string.
Syntax of the function -> Find( FindString, WithinString [, StartingPosition ] )
Find(“<a href=”, lblRawText.Text ) returns the starting position of the search phrase in the original string.
- We are then using the Right function of Power Apps. The Right function extracts specified number of characters from the end of the string.
Right( lblRawText.Text, Len(lblRawText.Text) – Find(“<a href=”, lblRawText.Text) – 8 )
Anything to the right of <a href=” is extracted from the string using this formula. I hope you already guessed why I subtracted 8 from the position returned by Find function.
Find function return the starting position of a search phrase. I wanted to get the entire string content to the right of the search phrase. In our case it is <a href=” which is 8 characters
- And now the utility of the With function. Using With I am temporarily holding the entire string inside a variable urlRightChars to be used in the next step.
Let’s now concentrate on the next part of the formula.
With(
{
replacePosition: Find(
$"{Char(34)}",
urlRightChars
) - 1
}
This is bit simpler. Here I use the With function again to store the first occurrence of the ” character in the string I extracted in earlier step. From the start of the string till the next ” double quote character, is my desired output.
Observe I have used the Char(34) function to represent double quote since double quote is a special character in Power Apps.
Now I have the position of the first occurrence of the double quote. And now comes the final step.
Left(
urlRightChars,
replacePosition
)
This time I use the Left function to fetch all the characters to the left of the double quote. If you have proceeded this far, I am pretty sure you are pretty much accustomed with these functions now and their way of working.
I hope this helped.
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.