Replace Last Character from String in Power Automate

For a few days, I have been working on the Replace() function in Power Automate, which allows replacing characters in a string, substrings within a string, strings within an array, and strings within a JSON array, as well as multiple objects within an array.

This tutorial explains how to replace the last character of a text string using Power Automate.

Here, I will demonstrate two examples:

  • Replace the Last Character of a String with Another Character
  • Replace the Last ‘N’ Characters of the String with a Blank

Replace the Last Character of a String with Another Character in Power Automate

To explain this, I will take a text string, such as ‘Customer Name: CHRISTINE ANNABELLE‘, and store it in a variable. From the string, I wanted to replace the last character with another character. I.e., CHRISTINE ANNABELLA.

You can achieve this by following the steps below:

  1. Let’s create a Power Automate Instant cloud flow that can be triggered by a single button. If you are new to Power Automate, check out Instant cloud flow in Power Automate.
  2. To store the text string, add an Initialize variable. Set the name and Type as ‘String’ and enter the customer’s name as the Value.
power automate replace last character from string
  1. Then, add a ‘Compose‘ action to provide the expression that will replace the last character with another character. Insert the following expression in the Inputs parameter.
concat(substring(variables('VarCustomerName'), 0, sub(length(variables('VarCustomerName')), 1)), 'A')

In the expression above, you can replace the variable with your variable.

Replace last character in String Power Automate
  1. The flow is ready now. Save the Flow actions. Then, click on the Test icon to test the flow.
  2. After the flow runs successfully, you can see that the last character of a string is replaced with another character [CHRISTINE ANNABELLE -> CHRISTINE ANNABELLA]

Output:

Replace Last character of text string Power Automate

This is how you can replace the last character from a text string using Power Automate.

Now, let’s see how to replace the last ‘n’ characters from a string in the example below.

Replace Last ‘n’ Characters with Blank in Power Automate

In this example, I will take a file name as a string from the SharePoint Document library, where the files are stored in the following format:

https://szg52.sharepoint.com/sites/HumanResourcesDepartment/InvoiceDocuments/Reports/01.jpg

From the given string above, I want to replace the last six characters [01.jpg] with a blank, resulting in the updated Value like:

https://szg52.sharepoint.com/sites/HumanResourcesDepartment/InvoiceDocuments/Reports

To do this, I will create a Power Automate Instant cloud flow.

Check out the steps below:

  1. In Power Automate, add an Instant cloud flow with a manually triggered flow trigger.
  2. Then, declare a variable that will store the string. For that, take an ‘Initialize variable‘ action. Provide a name for the variable, set the Type as string, and the Value with the text string value.
Power Automate replace last character of string
  1. After that, add a ‘Compose‘ action with the expression given below.
concat(substring(variables('VarFileURL'), 0, sub(length(variables('VarFileURL')), 6)), '')

In the expression above, you can change ‘6’ with the Value representing the number of characters you want to replace.

Replace last character in Power Automate String

Now, save and run the flow manually.

  1. You can see the result in the compose outputs, where the last ‘n’ characters have been replaced with a blank value.

Output:

Replace Last n characters of string Power Automate

This is how to replace the last characters from a string using Power Automate.

I hope you found this tutorial informative and helpful. If you have any suggestions for me, please leave your comments in the section below.

You can check out the following tutorials:

>

Build a High-Performance Project Management Site in SharePoint Online

User registration Power Apps canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App

Power Platform Tutorial FREE PDF Download

FREE Power Platform Tutorial PDF

Download 135 Pages FREE PDF on Microsoft Power Platform Tutorial. Learn Now…