Replace in Power Apps | PowerApps Replace Character in String

In this Power Apps article, I will tell you what the PowerApps replace function is, how to use Power Apps replace function, and how to substitute in Power Apps with various examples.

Also, we will discuss how to work with Power Apps replace character in string, Power Apps remove special characters from string, and many more like:

  • Power Apps Replace Space
  • Power Apps Find and Replace Character in String
  • Working with Power Apps Replace Double Quotes
  • How to use PowerApps Replace Line Break

Replace Function in Power Apps

Power Apps Replace function helps to identify the string to replace by starting position and length. Below are the Power Apps Replace function syntaxes:

Syntax-1:

Replace( String, StartingPosition, NumberOfCharacters, NewString )

Where,

  • String = This is the required field that defines the string to operate on
  • StartingPosition = This is the character’s position to start the replacement. The first character of String is at position 1
  • NumberOfCharacters = The number of characters to replace in String
  • NewString = This is the replacement string. The number of characters in this argument can differ from the “NumberOfCharacters” argument

Syntax-2:

Suppose you pass a single-column table that contains texts; the return value is a single-column table of modified strings. If you have a multi-column table, you can shape it into a single-column table.

Replace( SingleColumnTable, StartingPosition, NumberOfCharacters, NewString )

Where,

  • SingleColumnTable = A single-column table of strings to operate on. This is a required field

Substitute Function in Power Apps

Power Apps Substitute function helps to identify the string to replace by matching a string. You can replace the text if more than one match is found. Below are the Power Apps Substitute function syntaxes:

Syntax-1:

Substitute( String, OldString, NewString [, InstanceNumber ] )

Where,

  • String = This is the required field that defines the string to operate on
  • OldString = This is the required field that defines a string to replace
  • NewString = The replacement string. OldString and NewString can have different lengths
  • InstanceNumber = This is optional. If String contains more than one instance of OldString, use this argument to specify which instance to replace. Otherwise, all instances will be replaced

Syntax-2:

Substitute( SingleColumnTable, OldString, NewString [, InstanceNumber ] )

Where,

  • SingleColumnTable = A single-column table of strings to operate on

Power Apps Replace Character in String

In Power Apps, if you want to replace a character in a string, follow the simple examples below.

1. Power Apps Replace a Single Character in String

Suppose you want to replace the second character [“e”] in “Hello” with another character [“a”]; you can follow the below two codes.

1. Text = Replace("Hello", 2, 1, "a" )
replace in powerapps
2. Text = Substitute("Hello", "e", "a" )
substitute in powerapps

2. Power Apps Replace Multiple Characters in String

If you want to Replace four characters in “Johannal” with a single “#” character, starting with the second character (“o”), Refer to the code below.

Text = Replace("Johannal", 2, 4, "#" )
replace function in powerapps

Also, you can use the code below to replace multiple occurrences in “power platform” [p->P].

Text = Substitute("power platform", "p", "P")
powerapps replace function

3. Power Apps Replace a Substring in String

Suppose you want to substitute a substring in the text [Sales Products], such as substituting the string “Cost” for “Product.” Follow the code below.

Text = Substitute( "Sales Product", "Product", "Cost" )
powerapps replace character in string

Similarly, you can also replace the last two characters of “2024” with “22” using the code below.

Text = Replace("2024",3,2, "22")
power apps replace character in string

4. Power Apps Replace with an Empty String to Remove Characters

In this example, I want to remove some characters [PowerApps] from the string “Microsoft PowerApps.” In this case, we can use an empty string, as shown below.

Text = Substitute("Microsoft PowerApps" , "PowerApps", "")
power apps replace function

In the same way, you can also replace the first three characters of “658712” with an empty string[“”] using the code below.

Text = Replace( "658712", 1, 3, "" )
substitute powerapps

5. Power Apps Replace a String of Characters using Variables

Here, I will show you how to replace characters in a string using variables. To do so, follow the below steps. Such as:

1. To create a variable, select the App object [From the left navigation] and set its OnStart property to the code below.

OnStart = Set(
    varText,
    "powerapps"
);
Set(
    varNewText,
    Substitute(
        varText,
        "powerapps",
        "Power Apps"
    )
)

Where,

  • varText, varNewText = Power Apps global variable names
  • powerapps” = We can replace this string of characters with the proper use case [Power Apps]
replace in power apps

2. On the Power Apps Screen -> Insert a Text label and set its Text property as:

Text = varNewText

This way, you can work with the Power Apps to replace characters in strings using variables.

replace powerapps

Power Apps Remove Special Characters from String

In this scenario, I will show you how to remove special characters from a string or how to validate a field for a special character in PowerApps. Let’s take a simple scenario to understand better.

1. In Power Apps, there is an Edit form that connects it to the SharePoint list. In the form, there are a few fields that I want to validate. If a user enters a special character (like \, *, #, etc.), then the form will not submit, and it will show a warning message.

2. I can use the Power Apps Notify function for warning messages, which will determine whether the field value contains a special character.

3. First of all, Set the OnChange property of the Text input control data card value by the given below formula:

OnChange = IsMatch(DataCardValue9.Text,".*[\\\"&Char(34)&"].*")

Where,

DataCardValue9.Text = Power Apps text input control name

powerapps remove special characters from string

4. To warn the user of invalid characters, Take a Label input control and apply the below formula on its Text and Visible property as:

Text = "Special characters are not allowed!"
power apps replace special characters
Visible = IsMatch(DataCardValue9.Text,".*[\\\"&Char(34)&"].*")
powerapps find and replace character in string

5. Now, select the Button control and set its DisplayMode property to the code below.

DisplayMode = If(
    !lbl_Text.Visible,
    DisplayMode.Edit,
    DisplayMode.Disabled
)

Where,

lbl_Text = Label input control name

powerapps replace special characters

6. Save, Publish, and Preview the app. Enter the text (in the text input control), including the special character [“\”].

Once you enter the text with the special character, a warning message will appear (in the label control), and at the same time, the Submit button will be disabled, as shown below.

This way, we can work with Power Apps remove special characters from string.

powerapps replace string

Power Apps Replace Space

Suppose in Power Apps, there is a Text input control and a Text Label control. Whenver the user enters any text or number into the text input field, including spaces. The text label will display the text without spaces.

For that, I am using the formula below on the Text property of the text label control.

Text = Substitute(txt_EnterText.Text," ","")

Where,

txt_EnterText= This is the text input control where the user will enter the string or text

powerapps replace space

Once it is done, Preview the app. Once the user enters any text or number into the text input field, including spaces. The text label will display the text without spaces.

Output:

power apps replace space

Power Apps Find and Replace Character in String

To find and replace characters in a string in Power Apps, go to the left Search section. You will see two options: [Find and Replace], as shown below.

powerapps find and replace character in string

Now, select the Replace option, search the existing text [varNewText] on the search box, and replace it with a new string value [varText] in the text input box. And finally, select the Replace All button.

power apps find and replace character in string

Once you click on the “Replace all” button, the confirmation message box will appear [Replace 2 instances across the app with ‘varText’]. Then, click on the Replace button.

Now, see the screenshot below for the output.

replace string powerapps

Power Apps Remove Text from String

Let’s see how to remove text from the string in Power Apps. To do so, follow the below steps.

1. On the Power Apps Screen -> Insert a Text input control and set its Default property to the code below.

Default = "Microsoft Power Apps"
powerapps remove text from string

2. Now, insert a Text label control and set its Text property as:

Text = Substitute(
    txt_EnterText.Text,
    "Microsoft",
    ""
)

Where,

  • txt_EnterText = power Apps text input control name
  • “Microsoft” = This is the text that I want to remove from the string
power apps remove text from string

3. Now, Preview the app; the text label control removes the specified text and displays the remaining string or text, as shown below.

This way, you can remove the text from the string in Power Apps.

power apps remove character from string

Power Apps Replace Double Quotes

In this example, We will check how to work with PowerApps replace double quotes.

  • Suppose you have a Label input control. Inside the label control, you want to display the string as “POWERAPPS” (with a double quote).
  • I tried the formula as “POWERAPPS” (on the Label Text property), but it didn’t work for me, as shown below.
PowerApps replace double quotes
PowerApps replace double quotes
  • To workaround with this, I have tried the solution below:
Text = Char(34) & "POWERAPPS" & Char(34)

Where,

“PowerApps” = Specify a string that you want to display in the label control.

  • Double quotes within PowerApps have different meanings other than the character. If you want to display a character in Text, we could make use of the Char() function, which would translate a character code into a string.
PowerApps replace double quote
PowerApps replace double quotes

PowerApps Replace Line Break

Here, We will see how to remove the End of Line character from a string in Power Apps.

Normally, we are using “/br” code to break a line or sentence in HTML. Similarly, in Power Apps, we can use the character code “Char(10)” for a line break. Let’s take a simple example.

  • In PowerApps, I have a Text input control (multiline) and a Label input control.
  • I want to remove multiple lines from a string, so I need to strip out the end-of-line character and make it all one line. For delimitation, we can add a comma.
  • All the output I need to display in the label control. So select the Label control and apply the below formula on its Text property as:
Text = Substitute(TextInput2.Text,Char(10),",")

Where,

  1. TextInput2 = Text input control name
  2. Char(10) = This is the character code for line breaking
  3. “,” = It is the delimit that I want to add

NOTE:

Not only you can use only the text input control (multiline), but also you can use the HTML text input control in Powerapps. You can use the above formula on the HTML Text property of the HTML Text input control.
PowerApps replace Line break
PowerApps replace Line break
  • Now, Save and Preview (F5) the app. Enter some multiple sentences inside the text box. The result will appear in the Label control without a break, including a delimiter “,”.
PowerApps replace Line breaking
PowerApps replace Line break

Some more articles you may also like:

I hope you find this post useful for you. This tutorial taught in detail information about the Power Apps Replace and Substitute functions with different examples. Such as:

  • Power Apps Replace Character in String
  • Power Apps Remove Special Characters from String
  • Power Apps Replace Space
  • Power Apps Find and Replace Character in String
  • Power Apps Replace Double Quotes
  • PowerApps Replace Line Break
>