Do you need to remove the first or last character from a string in Power Automate?
This operation can be crucial in data manipulation and processing. For example, you might need to remove the first or last character from a product code as this might represent something such as a color code for the product.
Removing starting or ending characters can be achieved with the use of some string functions.
This article will explore the expressions required to remove the first or last characters from a string in Power Automate.
Remove a Single Character from a String
All the examples will remove characters from a text string in a Compose action. This is referenced by outputs('Compose')
.
Remove the Last Character
substring(outputs('Compose'), 0, sub(length(outputs('Compose')), 1))
You can use the above expression to remove the last character from a string.
Replace 'Compose'
with the name of your action that contains the string. This expression calculates the length of the string and removes the last character by using the substring function.
This example will result in the string ABCDEF
.
Remove the First Character
substring(outputs('Compose'), 1, sub(length(outputs('Compose')), 1))
Try the above expression to remove the first character from a string.
Again, replace 'Compose'
with the name of your action that contains the string. This expression starts with the second character and captures the rest of the string.
This example will result in the string BCDEFG
.
Remove the First and Last Characters
substring(outputs('Compose'), 1, sub(length(outputs('Compose')), 2))
Use the above formula to remove both the first and last characters from a string.
This expression removes the first character by starting at the second character and then subtracts 2 from the length of the string, therefore removing the last character as well.
Replace 'Compose'
with the name of your action that contains the string.
This example will result in the string BCDEF
.
Remove Multiple Characters from Strings
These expressions can be modified slightly to remove any number of characters.
Remove the Last N Characters
substring(outputs('Compose'), 0, sub(length(outputs('Compose')), N))
Use the above substring expression combined with the length function to remove the last N characters from a string in Power Automate,
Replace N with the number of characters you want to remove.
substring(outputs('Compose'), 0, sub(length(outputs('Compose')), 2))
For example, the above expression will remove the last 2 characters from your string.
This expression calculates the current length of your string, subtracts N characters, and returns a new substring starting from the first character and ending at the new length.
This example will result in the string ABCDE
.
Remove the First N Characters
substring(outputs('Compose'), N, sub(length(outputs('Compose')), N))
Removing the first N characters from a string in Power Automate can be done using the above substring function. In this case, replace N with the number of characters you wish to remove.
substring(outputs('Compose'), 2, sub(length(outputs('Compose')), 2))
For example, the above expression will remove the first 2 characters.
This expression will create a new substring starting from the Nth character and ending at the original length of your string.
This example will result in the string CDEFG
.
Remove the First N and Last M Characters
You can also combine the removal of the first and last characters into a single expression.
substring(outputs('Compose'), N, sub(length(outputs('Compose')), add(N, M)))
You can combine the techniques from the previous sections to remove the first N and last M characters from a string with the above expression.
Use the following above expression by replacing N with the number of characters you want to remove from the beginning, and M with the number of characters you want to remove from the end.
substring(outputs('Compose'), 2, sub(length(outputs('Compose')), add(2, 3)))
For example, the above expression will remove the first 2 characters and last 3 characters from the string.
This example will result in the string CD
.
Conclusions
Removing the last character from a string in Power Automate can be achieved using the substring function.
The substring function allows you to extract part of a text based on a start and end index number for the character position.
This means it can be used to remove the first or last number of characters.
Have you ever used this technique to remove characters? Let me know in the comments!
use slice function
Ah, yes. I thought substring and slice were more or less equivalent. But slice has the ability to use a negative index which will simplify things quite a bit.
I might add that solution soon. Thanks for the tip.