$-Strings: A Better Way To Concatenate Text Strings In Power Apps

$-Strings: A Better Way To Concatenate Text Strings In Power Apps

An awesome new way to concatenate text strings in Power Apps has arrived with $-Strings. Placing the $ symbol in front of any text string now defines it as an $-String. Then you can write any function, expression of variable inside a within a pair of curly braces { } inside the text string. I love this syntax and I can’t wait to share it with the world. In this article I will show you how to use $-Strings to concatenate a text string in Power Apps.

Important – this feature first appeared in Power Apps version 3.22041.23. If you don’t see the new feature update your authoring version in the Power Apps studio settings.


Table Of Contents

The New Way To Concatenate Text With $-Strings$-String Usage Examples
   Referencing Controls
   Reading Variable Values
   Math Expressions
   Concat Function
   Error HandlingOther $-String Details
   Curly Braces
   Quotation Marks




The “Old Way” To Concatenate Text Strings In Power Apps

A common thing we want to do in Power Apps is show the app user a welcome message with their name and the current date.

Hello, Matthew Devaney. The current date is 4/10/2022.



The message is a made up of two elements: text used to define the standard greeting users will see and functions used to obtain their name and the current date. To display these elements as a single message we can use the CONCATENATE function to join them together. Each section of text or function is a supplied as a new argument separated by a comma. It works, but the code itself is somewhat verbose and not very readable.

Concatenate(
    "Hello my name is",
    User().FullName,
    ". The current date is",
    Today(),
    "."
)



Another way to join multiple text strings and functions together is with & operator. Instead of writing a CONCATENATE function we simply write an & symbol between each section just like Microsoft Excel. This more readable during a code review but I find it a pain to type the extra & symbol and quotation mark between each segement.

"Hello my name is" & User().FullName &". The current date is" & Today() & "."




The New Way To Concatenate Text With $ Strings

My preferred way to concatenate text strings in Power Apps is by using the new $-String syntax. To do this we put a dollar sign ($) in front the text string’s quotation marks and use curly braces {} to denote the parts of it which are expressions – functions, formulas or variables that might change. The official term for this technique is string interpolation and it is found in many other programming languages.

$"Hello {User().FullName}. The current date is {Today()}."



I enjoy $-Strings because they require the least amount of syntax to write and my code is easily readable when reviewing it.



$ String Usage Examples

Let’s take a look at few more examples of how we can use $-Strings to concatenate text in Power Apps.




Referencing Controls

We can join the text from a text input and selected date from a date picker to other text strings like this:

$"Hello {txt_Username.Text}.  The current date is {dte_CurrentDate.SelectedDate}."



The result looks like this:




Reading Variable Values

We could also choose to store the result some functions in variables and use those variables inside the $-String.

// set variables elsewhere in the code
Set(varUsername, User().FullName);
Set(varToday, Today());

// use this value in the Text property of a label
$"Hello {varUsername}.  The current date is {varToday}."


Math Expressions

Writing mathematical expressions inside of $-strings is an interesting use case. Anything inside the curly braces can be a formula so we can directly write 4 * 3 inside the text string to find the result of 12.

$"{4} multiplied by {3} equals {4*3}"



Here’s a similar example of how we could multiply two values found within text inputs and show the result

$"{txt_Number1.Text} multiplied by {txt_Number2.Text} equals {Value(txt_Number1.Text) * Value(txt_Number2.Text)}"



Concat Function

The $-String is quite valuable when using the CONCAT function. CONCAT joins the result of a formula applied to all records in a table and outputs a single text string. In this example I have created a table holding the numbers 1-through-5 and want to know their squares.

Concat(Sequence(5), $"{Value} squared equals {Value * Value}.{Char(13)}")



The resulting text string looks this:

1 squared equals 1
2 squared equals 4
3 squared equals 9
4 squared equals 16
5 squared equals 25




Error Handling

One final use case for $-Strings is error-handling. We can place the following code within the OnError property of an app to show the developer a helpful error message.

Notify(
   $"Error was observed at {Error.Observed}: {Error.Message}",
   NotificationType.Error
)



The resulting error message would look something this. This specific error message is saying the app cannot perform division by zero and the value entered in the text input txt_Number is causing the problem.

Error was observed at txt_Number: Cannot divide by zero




Other $-String Details

When concatenating text with Power Apps $-String notation you might have the following difficulties.



Curly Braces

Curly braces are rarely needed inside text strings but what if we wanted to display a message like this?

This is how you write curly braces {} in Power Apps $-Strings.




We can show curly braces inside of an $-String by placing one pair of curly braces inside another.

$"This is how you write curly braces {{}} in Power Apps $-Strings."






What if a word needed to be shown within curly braces?

I can also write words inside curly braces {like this}




Again, we simply have to write two-pairs of curly braces with the word in the middle.

$"I can also write words inside curly braces {{like this}}"




Quotation Marks

Quotation marks are another challenge because they are used to identify text strings. What if we wanted to write the sentence below?

The English word "Hello" has the same meaning as the French word "Bonjour"



By writing 3 sets of quotation marks we can tell Power Apps to display a single-set of quotation marks surrounding the words “Hello” and “Bonjour”.

$"The English word {"""Hello"""} has the same meaning as the French word {"""Bonjour"""}"





Questions?

If you have any questions about $-Strings: A Better Way To Concatenate Text Strings In Power Apps please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest
47 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Radek
Radek
4 months ago

Hi, are there any usecases where old way would not work?
Thanks

Vipin
Vipin
4 months ago

Enjoyed your article Matt. Please notice a small mistake in Set(varToday); line.

David Stone
David Stone
4 months ago

Matt, I can’t seem to get this to work – I’ve tried as simple as: –

$"test"

And still get an error. Am I missing something? Is the dollar character different according to regions?

David Stone
David Stone
4 months ago

That’s great, thanks!

Do you have an examples of how you would conditionally insert extra text into the middle of one of these strings? For example I could currently do: –

"String" & If(true, "ent") & " something"
Greg Lindhorst
Greg Lindhorst
3 months ago
Reply to  David Stone

$”String {If( true, “ent”)} something”

Juli
Juli
2 months ago
Reply to  Greg Lindhorst

I am getting an error trying to do this. In my case I am adding a dash and then the value of another text input. I am not sure I am doing it right though as I am getting the “Expressions which appear inside an interpolated string must evaluate to a Text value or to a compatible type”

So this:

$”##{tiCountry.Text} {tiCity.Text} – {tiEquipmentName.Text}”

Needs to be this:

$”##{tiCountry.Text} {tiCity.Text}{If(cbMultipleBuildings.Value, “-” & tibuildingidentifier.Text)} – {tiEquipmentName.Text}”

Sultan
Sultan
4 months ago

Very cool article, I didn’t know about the $ sign before

George Winters
George Winters
4 months ago

OK, but how do you pronounce “$-String”?

George Winters
George Winters
4 months ago

I’m going with “String-string”, but that’s because I’m old.

Greg Lindhorst
Greg Lindhorst
3 months ago
Reply to  George Winters

We are calling it String Interpolation, the same term used by C#. And if you Bing the definition of interpolation (which I had to do), it fits pretty well: “the insertion of something of a different nature into something else.”

Gus ChessMan
Gus ChessMan
4 months ago

Nice,
Time to update my apps.
What about HTML texts, how it works?

Gus Chessman
Gus Chessman
4 months ago

Tx,
I have prove the concept and works prefectly!

Gus Chessman
Gus Chessman
4 months ago
Reply to  Gus Chessman

Everything comes a mess when published.
For some reason, PowerApps did not publish the App correctly, even if during editing, everything looks correct.

Andrew Holding
Andrew Holding
4 months ago

Another great write up. Thanks for taking the time.

Reiner Knudsen
Reiner Knudsen
4 months ago

$”Hi Matthew, love it sooo much 🙂 Especially when putting together URLs or HTML from data in the app that is so much easier to read and proof. Thanks for sharing.{so_happySmilingFace}”

Kathrine
Kathrine
4 months ago

Hi Matt, after reading your article I’ve given it a try and I have some strange things happening once I close the app. All works fine but when I save and close the app and come back to it later all of my $ have been automatically changed to the word Concatenate and my whole app is covered in errors!

E.g my code was: Launch($”tel:{Label1_2.Text}”)
After saving, closing & re-opening it no reads: Launch(Concatenatetel:{Label1_2.Text}”)

Any ideas why?

Gus Chessman
Gus Chessman
4 months ago

I have had the same problem that Kathrine have.
By the way, I changed some codes in my app with de $-Strng approach, everything looked great, but when open the app was obligated to return to old way, cause code was chaged automatically to what exactly Kathrine described.

Rob Agnew
3 months ago

I’m on Authoring version 3.22041.27 btw

Rob Agnew
3 months ago

I replaced:
        Claims: Concatenate(“i:0#.f|membership|”, currentUser.Email),
with
 Claims: $”i:0#.f|membership| {currentUser.Email}”
I then saved, Published and exited. When I restart the code then shows;
Claims: Concatenatei:0#.f|membership| {currentUser.Email}” , 
and is broken.
After correcting back to the original all is well.

However… I created a new app using your example
// set variables elsewhere in the code
Set(varUsername, User().FullName);
Set(varToday, Today());

// use this value in the Text property of a label
$”Hello {varUsername}. The current date is {varToday}.”
I then created another label with
Concatenate(“Hello ” & varUsername , “. The current date is ” & varToday)
Saved, Published and reloaded both labels stayed the same.
I then changed the Concat to the $ string then etc… and it remained intact!

So I’m baffled! Perhaps it may have something to do with the fact that the app I changed pre-dates the update? The fact remains that I can’t use the $ String in an existing app….

Rob Agnew
3 months ago
Reply to  Rob Agnew

I should point out that the $ string change worked fine before saving & publishing & reloading on the old app

Greg Lindhorst
Greg Lindhorst
3 months ago
Reply to  Rob Agnew

There was unfortunately a bug in the initial release of this feature that causes this behavior in some scenarios. It has been fixed in 3.22051 which will be available soon.

Miroslaw Magierski
Miroslaw Magierski
3 months ago

Hi Matt,
Thanks for the article – that’s very cool!
However, I can report the same issue as others. $” (dollar sign and the initial quotation mark) get replaced by the word Concatenate upon saving my app.
I’m on version 3.22041.27, but my app predates it.
I’ve initially changed some of my HTML Text fields (it worked wonderfully) only for them to be broken when I saved and re-opened the app. I also tested it in the same app on a plain Label field with a very simple string and observed the same behaviour.

Greg Lindhorst
Greg Lindhorst
3 months ago

There was unfortunately a bug in the initial release of this feature that causes this behavior in some scenarios. It has been fixed in 3.22051 which will be available soon.

Miroslaw Magierski
Miroslaw Magierski
3 months ago
Reply to  Greg Lindhorst

Thanks, Greg! Appreciate you taking the time to inform everyone individually. Looking forward to the new release.

Kathrine
Kathrine
3 months ago

Hi Matthew,
So sorry I’ve only just seen your response, I am on authoring version 3.22042.8 and it was a brand new app that I created the same day I made my first comment.

Reiner Knudsen
Reiner Knudsen
3 months ago
Reply to  Kathrine

I can confirm this behaviour. I also made some changes and came back to the same experience Katherine had.
Plus: The German version of make.powerapps.com uses (don’t ask me why) semicolons when the English version uses commas in the code. When I saved my app and opened it again not only had the editor replaced the $ with ‘Concatenate’ but it also had replaced all (!) semicolons with commas. Quite a long of editing to do for me 😕

Rob Agnew
3 months ago
Reply to  Kathrine

Hi Matt,

Thanks for the heads up on the $ interpolation feature – been using this for years in my other environments. However, I can report exactly the same issue as others have described; after publishing or reloading the $ is replaced by Concatenate in every instance!

Andrew Ng
Andrew Ng
3 months ago
Reply to  Kathrine

I have the same issue and am on authoring version 3.22042.8

Valdemir
Valdemir
3 months ago

Hi Matthew, very cool feature! Would make coding quite a bit simpler and easier to read if it worked properly 😅.
Works perfect in Power Apps Studio. But the strings are invisible after publishing the app.
Im on authoring version 3.22041.27.

David Stone
David Stone
3 months ago

Hi Matthew. Have you tried this in a HTML label? What I’m finding is it ‘works’ in the studio, but when I play the app after publishing, the label defaults to the original placeholder text for a HTML label.

Andrew Ng
Andrew Ng
3 months ago

I not sure if this is a bug, but I found after I use $ string and close and reopen the app, all of my $ strings converted from ‘$”‘ to ‘concatenate’ and threw errors because now the syntax is wrong. Wondering if anyone come across this issue. I am on authoring version 3.22042.8

Last edited 3 months ago by Andrew Ng
Greg Lindhorst
Greg Lindhorst
3 months ago
Reply to  Andrew Ng

There was unfortunately a bug in the initial release of this feature that causes this behavior in some scenarios. It has been fixed in 3.22051 which will be available soon.

Andrew Ng
Andrew Ng
3 months ago
Reply to  Greg Lindhorst

thanks for the update. Good to know

Clinton
3 months ago

Used it for the first time and things didn’t go well. Opened the project the next day and it redid all the formulas. Below is an example of what happened, unfortunately I used the new format quite a bit and am in the process of changing them to the way I know works.
How I put it in: $”Hello{Char(10)}World”
What it changed too: ConcatenateHello{Char(10)}World”
I did check version and I’m on 3.22044.32, any recommendations?

Jeff
Jeff
2 months ago

Great article! Thanks for sharing.
How would I code a carriage return to place text on the next line?

Jeff
Jeff
2 months ago

Thank you!

Shefi
Shefi
3 days ago

Hi Matt

I have 3 columns in the sharepoint list, which I want to combine using a powerautomate flow.

 

In the flow, I am using Concat function as below with a separator

 

concat(triggerOutputs()?[‘body/Group1′],’, ‘,triggerOutputs()?[‘body/Group2′],’,’ ,triggerOutputs()?[‘body/Group3’])

 

Group1: Test 1, Test 2

Group2: empty column

Group3: Test3, Test4

 

The output is: Test 1, Test 2, ,Test3, Test4

 

I need to remove the extra separator if any of the columns are empty.

 

Thanks in advance