Power Automate Substring Function Explained By Examples

power automate substring

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.

  1. Create a new flow with a manual trigger

    Power Automate Substring New Flow

    Power Automate – Create new flow

  2. Add an input to the trigger

    Power Automate Substring Add input to trigger

    Add an input

  3. Select text as input parameter type

    Power Automate Substring add text input to trigger

    Text input

  4. Add an Initialize variable action to the flow

    Power Automate Substring Add Initialize variable

    New step Initialize variable

  5. Set Name to ‘text’, Type to ‘String’ and reference ‘Input’ as value.

    Power Automate Substring setup variable

    Initialize variable configuration

  6. Add a Compose action to the flow

    Power Automate Substring Add Compose action

    New Compose step

  7. Set the expression for Inputs to: substring(variables(‘text’),0,3)

    Power Automate Substring

    Compose setup

  8. Add a Substring action to the flow

    Power Automate Substring Action

    Substring action

  9. Set Text to reference the ‘text’ variable, Starting Position to ‘0’ and Length to ‘3’

    Power Automate Substring Action setup

    Configuration of Substring action

  10. Save & run the flow with input ‘Monday’

    Power Automate Substring Test Run

    Run flow

  11. Check the result of the test run

    Power Automate Substring Test Run Result

    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))

Leave a Comment

Your email address will not be published. Required fields are marked *