Power Automate Substring: How to Extract Text

How-to basics · Updated November 23, 2026

In Power Automate, the substring() function extracts a specific portion of a string by specifying a start index and a length, returning the characters between those positions. For example, substring('Hello World', 6, 5) returns 'World'. This function is essential for parsing text from emails, SharePoint columns, or API responses, and it works in both cloud flows and desktop flows (though desktop flows use a different syntax).

Understanding the substring() Function Syntax

The substring() function in Power Automate follows this syntax:

substring('<text>', <startIndex>, <length>)
  • text: The string you want to extract from. This can be a literal string (enclosed in single quotes) or a dynamic expression that returns a string.
  • startIndex: The zero-based position where extraction begins. The first character is at index 0.
  • length: The number of characters to extract. This must be a positive integer.

For instance, substring('Microsoft Power Automate', 10, 5) returns 'Power' because the character at index 10 is ‘P’ (counting from 0), and the next 5 characters are ‘o’, ‘w’, ‘e’, ‘r’.

You can also use expressions like substring(variables('myString'), 0, 3) to get the first three characters of a variable.

How to Use substring() in a Flow

To use substring() in a flow, you typically add it inside an expression in a Compose action, a condition, or a variable assignment. Here’s a step-by-step example:

  1. In your flow, add a Compose action (or any action that accepts dynamic content).
  2. Click in the Inputs field, then select the Expression tab.
  3. Type substring('The quick brown fox', 4, 5) and click OK.
  4. Run the flow. The output will be 'quick'.

You can also use dynamic content from triggers or actions. For example, to extract the first 10 characters from an email subject, you would use substring(triggerOutputs()?['headers']?['Subject'], 0, 10).

Choosing the Right Start Index and Length

Getting the correct start index and length is the most common challenge. Remember that indices are zero-based, so the first character is at position 0. The length counts characters starting from that index. For example, to extract the last 4 characters of a string, you need to calculate the start index as the total length minus 4. Power Automate provides the length() function to get the total number of characters.

Example: substring('abcdef', length('abcdef') - 2, 2) returns 'ef'. This works because length('abcdef') is 6, so the start index is 4, and the length is 2.

If you need to extract a substring between two known delimiters (like everything between parentheses), you can combine substring() with indexOf() to find the positions. For instance, to get the text between ‘(‘ and ‘)’ in 'Hello (World)':

substring('Hello (World)', indexOf('Hello (World)', '(') + 1, indexOf('Hello (World)', ')') - indexOf('Hello (World)', '(') - 1)

This returns 'World'.

Common Errors and How to Avoid Them

The most frequent error with substring() is an out-of-range index. If the start index is negative, or if the start index plus the length exceeds the string’s length, the flow throws an error like “The template language function ‘substring’ parameters are out of range.” To avoid this, always validate the string’s length before extracting. You can use a condition like:

if(greaterOrEquals(length(variables('myText')), 5), substring(variables('myText'), 0, 5), variables('myText'))

This checks if the string has at least 5 characters; if yes, extract the first 5; otherwise, return the whole string.

Another common mistake is using a negative length. The length parameter must be a non-negative integer. If you need to extract to the end of the string, you can use a large number like 1000, but it’s safer to use substring(text, startIndex, length(text) - startIndex).

substring() vs. Other String Functions

Power Automate offers several string functions, and knowing when to use each is key:

  • substring(): Extracts a fixed portion by index and length. Use when you know exactly where the text is.
  • split(): Divides a string into an array based on a delimiter. Use when you want all parts separated by a comma, space, etc.
  • replace(): Replaces occurrences of a substring with another. Use to remove or change specific text.
  • indexOf(): Returns the position of a substring. Use to find where something starts, often combined with substring().
  • startsWith() / endsWith(): Check if a string starts or ends with a certain substring. Use for simple prefix/suffix checks.

For example, to get the domain from an email address, you could use substring(email, indexOf(email, '@') + 1, length(email) - indexOf(email, '@') - 1). Alternatively, you could use split(email, '@')[1], which is simpler. Choose the method that makes your flow easiest to read.

Performance and Limits

In cloud flows, expressions like substring() are evaluated quickly, but there are limits on the size of strings you can process. The maximum string length in Power Automate expressions is around 100,000 characters, but for practical purposes, keep strings under a few thousand characters to avoid performance issues. Also, be mindful that using too many substring operations on huge strings can slow down your flow.

For desktop flows (Power Automate Desktop), the substring function is different. You use the Substring action from the Text group, which takes a text input, start index, and length. The syntax is similar, but it’s a separate action, not an expression.

Frequently Asked Questions

Can I use substring() on a variable that might be null?

No, substring() requires a valid string. If the variable is null, the flow will fail. Use the coalesce() function to provide a default value, like substring(coalesce(variables('myVar'), ''), 0, 3).

How do I extract text after a specific character?

Use substring(text, indexOf(text, 'char') + 1, length(text) - indexOf(text, 'char') - 1). For example, to get everything after the ‘@’ in an email, use substring(email, indexOf(email, '@') + 1, length(email) - indexOf(email, '@') - 1).

What if the start index is greater than the string length?

You’ll get an error. Always check the length first using a condition or the if() function to ensure the start index is within bounds.

Can I use substring() in a condition?

Yes, you can use it in a condition expression. For example, to check if the first 3 characters of a string are ‘ABC’, use equals(substring(variables('str'), 0, 3), 'ABC').

Does substring() work with Unicode characters?

Yes, it works with Unicode, but be aware that some characters (like emojis) may be represented by multiple code points. The function counts characters, not bytes, so it should work fine, but testing is recommended for complex scripts.

Is there a way to extract a substring from the end?

There is no direct ‘substring from end’ function, but you can calculate the start index as length(text) - n. For example, to get the last 5 characters: substring(text, length(text) - 5, 5).

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.