At some day everyone needs the Microsoft Power Automate Substring function to manipulate a string. This article will show you how to use the substring function in a flow, explain the syntax and provide some examples for common use cases. Furthermore the Power Automate Substring action is introduced as well.
How To Use Substring In Power Automate
The flow demonstrates how to use the Power Automate substring function and the Substring action. We will create a substring containing the first three characters of the given string. Note: This example flow will fail, if the given string has less than 3 characters.
-
Create a new flow with a manual trigger
Power Automate – Create new flow
-
Add an input to the trigger
Add an input
-
Select text as input parameter type
Text input
-
Add an Initialize variable action to the flow
New step Initialize variable
-
Set Name to ‘text’, Type to ‘String’ and reference ‘Input’ as value.
Initialize variable configuration
-
Add a Compose action to the flow
New Compose step
-
Set the expression for Inputs to: substring(variables(‘text’),0,3)
Compose setup
-
Add a Substring action to the flow
Substring action
-
Set Text to reference the ‘text’ variable, Starting Position to ‘0’ and Length to ‘3’
Configuration of Substring action
-
Save & run the flow with input ‘Monday’
Run flow
-
Check the result of the test run
Result test run
The example shows that the function and the action produce the same result.
Power Automate Substring Function
Use the substring function to extract a part of a given text.
Syntax
substring('<text>', <startIndex>, <length>)
Parameters
- text (mandatory): The string on which you want to extract a substring.
- startIndex (mandatory): The position within the given text where the substring should begin. Note the first character is on position 0.
- length (optional): The number of characters that should be in the substring starting at the given startIndex. If length is not provided, the substring will be from starIndex to the end of the text.
Notice: In case the length parameter results in a substring that exceeds the length of the given text there will be an error.
Return Value
- The extracted substring.
Examples
substring('Monday',0,3)
=> 'Mon'
substring('Monday',1,3)
=> 'ond'
substring('Monday',3)
=> 'day'
substring('Mo',0,3)
=> ERROR: The length of substring can't be longer than '2' which is the length of the source string.
Substring Examples
For some common use cases of the Power Automate Substring function we provide examples in this chapter.
Let me know, in case you are missing something.
Get substring before character
To use Power Automate substring to get a substring before a character of a string, you need to find the position of the searched character by using the indexOf function. The index of the character will be your substring length.
if(less(indexOf(variables('text'),'@'),0),variables('text'),substring(variables('text'),0,indexOf(variables('text'),'@')))
In case the searched character is not found, we will return the whole string.
First word
To use Power Automate substring to get the first word of a string, you need to find the first whitespace by using the indexOf function. The index of the whitespace will be your substring length.
In case there is no whitespace, we will return the whole string.
if(less(indexOf(variables('text'),' '),0),variables('text'),substring(variables('text'),0,indexOf(variables('text'),' ')))
Remove first character
To use Power Automate substring to remove the first character of a string, you simply need to set the start index to 1. Remember, the index starts at 0. Be careful with this, because an empty string will cause an error.
substring(variables('text'),1)
Remove last character
To use Power Automate substring to remove the last character of a string, you need to calculate the position of the last character by subtracting minus one of the total length of the string provided by the length function. The result will be your substring length.
substring(variables('text'),0,sub(length(variables('text')),1))
Extract last character
To use Power Automate substring to extract the last character of a string, you need to calculate the position of the last character by subtracting minus one of the total length of the string. The result will be your start index.
substring(variables('text'),sub(length(variables('text')),1))