Power Automate Substring: Syntax, 9 Examples, Error Fix
Power Automate substring syntax with copy-paste examples: first or last characters, text before or after a character, slice(), and the length error fix.
In this article · 29 sections
The Power Automate substring function returns part of a text value in a cloud flow expression. You give it the text, the position to start from, and how many characters you want:
substring(text, startIndex, length)
Counting starts at 0, so substring('hello world', 6, 5) returns world. Length is optional: leave it out and you get everything from startIndex to the end. The rule that breaks flows: startIndex plus length can’t run past the end of the text. If it does, the action fails with an error instead of returning a shorter string, as Microsoft’s expression reference warns (three fixes below).
h e l l o · w o r l d
0 1 2 3 4 5 6 7 8 9 10 11
no position 11 ↑
· is the space at position 5
substring('hello world',6,5) → world
substring('hello world',6,6) → error
slice('hello world',6,20) → world
Start 6 with length 5 covers positions 6 to 10 and ends on d, the last character: 6 + 5 = 11, exactly the length of the text, the most substring allows. Length 6 would need position 11, which doesn’t exist, so substring fails, while slice stops at the end of the text.
The Power Automate Substring Function: An Overview
Cloud flows share their expression functions with Azure Logic Apps, which is why Microsoft’s function reference for Power Automate sits in the Logic Apps docs. Desktop flows use their own text actions, such as Get subtext, and Power Apps uses Left, Mid, and Right, where Mid counts from 1, not 0.
Below: copy-paste recipes, a five-step demo flow, guards for a missing character or a blank value, and the fix for the length error.
What Is the Power Automate Substring Function Used For?
The substring function cuts a piece out of longer text. Two common jobs:
- Pull out an ID, a code, a name, or a domain from an email address, a file name, or a form field, for example a first name to place in a Word template.
- Cut a long value to a column’s limit before you save it. Microsoft’s error reference gives one such limit, 255 characters for a SharePoint single line of text column, and suggests substring() or take() for the cut. Substring fails on any value shorter than the limit, and Microsoft’s take() entry doesn’t say what take() does on one, so use slice (the fourth recipe below).
Microsoft’s expression cookbook says its expressions, substring included, work in all Power Automate cloud flow license tiers; for licensing questions, see is Power Automate free? and Power Automate pricing.
Copy-Paste Substring Examples: First, Last, Before, After a Character
Each expression reads a String variable named text; swap variables('text') for your own variable or dynamic content. The first three rows cut at fixed positions with substring; they’re the left, mid, and right patterns from Microsoft’s expression cookbook. The other six use slice, which takes a start and an end position and stops at the end of the text instead of failing: one caps the length, and five cut at a position that indexOf, nthIndexOf, or lastIndexOf finds. String functions work only on strings, so if the content is a number, wrap it in string() first, for example slice(string(<dynamic content>), 0, 3).
| You want | Expression | Example |
|---|---|---|
| First N characters (Left) | substring(variables('text'), 0, 7) | Invoice-2026-0042 → Invoice |
| Characters from the middle (Mid) | substring(variables('text'), 8, 4) | Invoice-2026-0042 → 2026 |
| Last N characters (Right) | substring(variables('text'), sub(length(variables('text')), 4), 4) | Invoice-2026-0042 → 0042 |
| Up to N characters (cut to a column limit) | slice(variables('text'), 0, 255) | a 300-character note → its first 255 characters; a 40-character note → unchanged |
| Text after a character | slice(variables('text'), add(indexOf(variables('text'), '@'), 1)) | alice@contoso.com → contoso.com |
| Text before a character | slice(variables('text'), 0, indexOf(variables('text'), '@')) | alice@contoso.com → alice |
| Text between two characters | slice(variables('text'), add(indexOf(variables('text'), '['), 1), indexOf(variables('text'), ']')) | Order [12345] shipped → 12345 |
| Text after the nth occurrence | slice(variables('text'), add(nthIndexOf(variables('text'), '-', 2), 1)) | Invoice-2026-0042-EU → 0042-EU |
| Text after the last occurrence | slice(variables('text'), add(lastIndexOf(variables('text'), '.'), 1)) | report.final.xlsx → xlsx |
Notes on the recipes:
- Length. The three substring rows need the text to be at least as long as the cut. For values that vary in length, use slice (more in the length error section).
- Negative start. For the last N characters,
slice(variables('text'), -4)is a shorter form for text of at least 4 characters, because a negative start counts back from the end. - Missing or repeated characters. If the character might be missing, repeated, or out of order, read the tradeoffs section below before you rely on these.
How to Use Substring in Power Automate: 5-Step Demo Flow
To see substring run, build this five-step flow. It stores a sentence in a variable, cuts the first word out of it with a Compose action, and shows the result in a test run. The steps use the new designer, the default for new flows.
Classic designer: add steps with the New step button at the bottom of the flow instead of the plus sign (Microsoft’s designer comparison).
Step 1: Create a New Instant Cloud Flow
- Sign in to Power Automate and select Create on the navigation pane to the left.
- Under Start from blank, select Instant cloud flow.
- On the Build an instant cloud flow screen, enter a name for the flow, for example Substring demo.
- Select the Manually trigger a flow trigger, and then select Create. The flow opens in the designer (Microsoft’s steps).
Step 2: Initialize a Variable
- Below the Manually trigger a flow card, select the plus sign (+).
- In the Add an action search box, enter initialize variable, and then select Initialize variable.
- In the Name field, type
string_1. - Choose String as the Type.
- In the Value field, enter
Citizen Development Academy power automate(Microsoft’s variable steps).
Step 3: Use Compose Action and Substring Formula
- Below Initialize variable, select the plus sign (+), search for compose, and under Data Operation, select Compose.
- Select the Inputs field, and then select the fx button to open the expression editor. Typing a forward slash (/) in the field and choosing Insert expression works too (Microsoft’s designer guide).
- Enter
substring(variables('string_1'), 0, 7)and select Add. This expression starts at index 0 and takes 7 characters, so it extracts the first word ‘Citizen’ from the string.
Step 4: Save and Test the Flow
- Select Save on the command bar. When there are no errors, the designer shows the message “Your flow is ready to go. We recommend you test it” (Microsoft’s designer guide).
- Select Test, select Manually, and then select Test (Microsoft’s test steps).
- In the Run flow pane, select Run flow, and then select Done (Microsoft’s test tutorial).
Step 5: Inspect the Output
- When the run finishes, a green checkmark appears on each card, together with the seconds it took (Microsoft’s designer guide).
- Select the Compose card to see its inputs and outputs (Microsoft’s run view).
- The output is
Citizen: the first 7 characters, which here are the first word.
To try a recipe from the table, add a second Initialize variable between the first one and Compose (select the plus sign between them), name it text, choose String, and set its Value to that row’s example text, for example alice@contoso.com. Paste the recipe into the Compose action unchanged, then save and test again. To see the trap the tradeoffs section covers, change variables('text') to variables('string_1') in the after-character recipe: the demo sentence has no @, so you get the whole sentence back and no error.
Break it on purpose. Set the Compose expression to substring(variables('string_1'), 28, 15) and test again. The run fails at Compose. Open the failed card to read the error (Microsoft’s troubleshooting guide), then try the three fixes.
Power Automate Substring Expression: Exploring the Complexities
To get text between two characters, find both positions with indexOf and pass them to slice:
slice(variables('text'), add(indexOf(variables('text'), '['), 1), indexOf(variables('text'), ']'))
Nested expressions run from the inside out: the inner function runs first and passes its result to the outer one, as Microsoft’s reference describes. On Order [12345] shipped:
indexOf(variables('text'), '[')returns6.add(6, 1)returns7, the first character after the bracket.indexOf(variables('text'), ']')returns12.slice(variables('text'), 7, 12)returns12345, because slice stops before the end index.
Read every nested recipe on this page the same way: innermost function first.
Power Automate Substring After Character: The Tradeoffs
To get a substring after a character, find the character’s position with indexOf, then take the rest with slice (or with substring and no length):
slice(variables('text'), add(indexOf(variables('text'), '@'), 1))
The add(..., 1) skips the character itself. Here is what each search returns on Invoice-2026-0042-EU:
| Search for | Position | Text after | Text before |
|---|---|---|---|
First - (indexOf) | 7 | 2026-0042-EU | Invoice |
Second - (nthIndexOf) | 12 | 0042-EU | Invoice-2026 |
Last - (lastIndexOf) | 17 | EU | Invoice-2026-0042 |
#, not in the text (indexOf) | -1 | Invoice-2026-0042-EU | Invoice-2026-0042-E |
The last row is the trap: both results look like ordinary text, and nothing fails.
The tradeoffs follow from how Microsoft’s function reference defines these functions:
- Multiple occurrences. indexOf returns the first match. Use lastIndexOf for the last one, and nthIndexOf for the second, third, and so on. nthIndexOf returns -1 when there are fewer matches than you asked for.
- Character not found. indexOf returns -1, and none of the indexOf and nthIndexOf recipes raises an error: the after and nth-occurrence recipes return the whole text and the before recipe drops the last character, as the last table row shows. Microsoft’s lastIndexOf entry doesn’t say what it returns for a missing character, so guard that recipe too.
- Blank value. A value you reference can be empty (null) instead of text, and Microsoft’s troubleshooting guide says to wrap such a reference in
coalesce(field, ''). Pair coalesce with slice:slice(coalesce(<dynamic content>, ''), 0, 7)returns an empty string for a blank value, whilesubstring('', 0, 7)still breaks the length rule. Dates need the same guard: see Power Automate formatDateTime.
To return an empty string when the character is missing, check indexOf for -1 first:
if(equals(indexOf(variables('text'), '@'), -1), '', slice(variables('text'), add(indexOf(variables('text'), '@'), 1)))
The check uses indexOf, not contains(), because contains() is case-sensitive and indexOf isn’t. With a letter as the delimiter, contains('1920X1080', 'x') returns false even though indexOf('1920X1080', 'x') finds the X at position 4. For a case-insensitive contains, wrap both values in toLower(), as Microsoft’s cookbook does. The same rule cuts the other way: indexOf and lastIndexOf match a letter in either case, so with a letter as the delimiter, a recipe built for x can cut at an X.
For the between recipe, check both characters. Without the check, a missing closing bracket gives a wrong slice: Order [12345 shipped returns 12345 shippe.
if(or(equals(indexOf(variables('text'), '['), -1), equals(indexOf(variables('text'), ']'), -1)), '', slice(variables('text'), add(indexOf(variables('text'), '['), 1), indexOf(variables('text'), ']')))
The guard checks that both characters are there, not their order. The recipe pairs the first [ with the first ], so a ] that comes before the first [ returns an empty string, as Microsoft’s slice example slice('Hello World', 10, 2) does, and nested brackets give a wrong cut. With the same character on both sides, end at the second match:
slice(variables('text'), add(indexOf(variables('text'), '-'), 1), nthIndexOf(variables('text'), '-', 2))
That turns Invoice-2026-0042 into 2026.
Make It Real: Get the Domain From a Trigger Input
The demo cuts a sentence you typed into the flow. In a real flow the text arrives from a trigger or an earlier action, and it can lack the character or be blank. This version reads an email address that someone types when they start the flow, and it gives each step its own Compose action, so the run shows every value, as Microsoft’s troubleshooting guide suggests.
- Select the Manually trigger a flow card. On its Parameters tab, select + Add an input > Text, and name the input
Email(Microsoft’s trigger input example). - Below the last action, add a Compose action and rename it
Find at: select the text Compose on the card’s title bar and type the new name (Microsoft’s Compose tip). - Below it, add a second Compose and rename it
Domain.
Enter these expressions in their Inputs fields, then replace <Email> with the trigger input: delete the placeholder, keep the cursor there, select the Dynamic content tab in the expression editor, and pick Email (Microsoft’s designer guide).
Find at:
indexOf(coalesce(<Email>, ''), '@')
Domain:
if(equals(outputs('Find_at'), -1), '', slice(coalesce(<Email>, ''), add(outputs('Find_at'), 1)))
Inside an expression, an action’s name takes underscores in place of spaces, so Find at is Find_at (Microsoft’s error reference). To see the reference the designer wrote for Email, select the card and open Code View (designer guide).
Test the flow with alice@contoso.com as the Email value: Find at shows 5 and Domain shows contoso.com, ready for the next step to route the request by the sender’s domain. Then test again with values from the best practices table: alice gives an empty Domain, and alice@contoso@example.com gives contoso@example.com, because indexOf stops at the first @.
Other String Operations in Power Automate
Microsoft’s reference lists most text functions under string functions and a few, such as contains, under collection functions. These are the ones you’ll pair with substring most, each with Microsoft’s own example:
| Function | What it does | Example and result |
|---|---|---|
indexOf | Position of the first match, counting from 0; -1 if not found | indexOf('hello world', 'world') returns 6 |
lastIndexOf | Position of the last match | lastIndexOf('hello world hello world', 'world') returns 18 |
nthIndexOf | Position of the nth match; -1 if there are fewer | nthIndexOf('123456789123465789', '1', 2) returns 9 |
slice | Text from a start index up to, not including, an end index | slice('Hello World', 2, 5) returns llo |
split | An array of the pieces between a delimiter | split('a_b_c', '_') returns ["a","b","c"] |
replace | Replaces a substring with new text; case-sensitive | replace('the old string', 'old', 'new') returns the new string |
trim | Removes leading and trailing whitespace | trim(' Hello World ') returns Hello World |
length | Number of characters in a string, or items in an array | length('abcd') returns 4 |
concat | Joins two or more strings | concat('Hello', 'World') returns HelloWorld |
contains | true if the text includes a value; case-sensitive | contains('hello world', 'world') returns true |
startsWith, endsWith | true if the text starts or ends with a value | endsWith('hello world', 'world') returns true |
toLower, toUpper | Changes the case | toLower('Hello World') returns hello world |
For ready-made patterns, Microsoft’s expression cookbook for cloud flows has 30 of them, from left, right, and mid substrings to replace and split.
The Power Automate Substring Function: Challenges
If a run fails with “The length of substring can’t be longer than ‘42’ which is the length of the source string”, with your text’s length in place of 42, startIndex plus length ran past the end of the text, even if neither number is too big on its own.
string_1 from the walkthrough holds 42 characters, so substring(variables('string_1'), 28, 15) asks for 15 characters starting at “power” when only 14 remain. Microsoft’s note on this rule says you then “get an error” instead of a shorter result. The note says the sum must be less than the length, but Microsoft’s own example, substring('hello world', 6, 5), ends on the last character and returns world, as does the cookbook’s Right pattern, so read the note as no more than the length.
A 2023 run of this demo that cut past the end of the same 42-character string failed with this message (the capture doesn’t show which start and length were used):
InvalidTemplate. The length of
substring can't be longer than
'42' which is the length of the
source string.https://aka.ms/
logicexpressions#substring

Three fixes, depending on what you meant:
- You want everything to the end. Leave length out:
substring(variables('string_1'), 28)returnspower automate. This needs the text to reach the start position. - The text may be shorter than you expect. Use slice, which stops at the end of the text instead of failing:
slice(variables('string_1'), 0, 50)returns the whole 42-character string. - You need substring itself. Cap the length with min():
substring(variables('string_1'), 28, min(15, sub(length(variables('string_1')), 28)))returnspower automate. The general form ismin(n, sub(length(text), start)), and with a start of 0 it’smin(n, length(text)).
min() only caps the length. When a value might not reach the start, sub(length(text), start) drops to 0 or below, which Microsoft’s parameter rules don’t allow (length must be a positive number), so use slice instead: slice(text, start, add(start, n)) cuts the same characters and returns an empty string when the start is greater than the text’s length, as Microsoft’s slice entry documents. slice(variables('string_1'), 28, 43) returns power automate.
When a run fails, open it and select the failed action to read its error message, as in the 2023 capture above (Microsoft’s troubleshooting guide); the values it worked on show in the outputs of the actions before it (Microsoft’s run view).
When Not to Use Substring: Free-Form Text, Patterns, JSON
- Free-form text. Text with no fixed position or marker, like an email body, is an extraction job, not a position job: see how the email-to-CRM flow extracts fields.
- Patterns. Microsoft’s string function list has no regular-expression function, so expressions can’t search for a pattern such as “any five digits”.
- JSON. Read values by name with json() or the Parse JSON action, one of the data operations next to Compose, instead of cutting JSON text by position.
- Filtering a list. To keep only the SharePoint items whose text contains a value, filter on the server instead: type an OData
substringofclause in the Get items Filter Query box (Microsoft’s Get items guide). SharePoint runs it; despite the name, it isn’t one of the flow’s expression functions. On a list with more than 5,000 items, turn on Pagination first, or the filter can return nothing, as Microsoft’s known limitation notes. How to Use Power Automate Filter Query has examples and the 5,000-item trap. More SharePoint actions: Power Automate for SharePoint.
Best Practices for Using Power Automate Substring Function
Implement Error Handling
In a cloud flow, three guards cover the failures on this page: slice for length (no length or a min() cap only when every value reaches the start), a -1 check for missing characters, and coalesce for blank values. For the rest of the text functions, see the Power Automate string functions cheat sheet.
Test Your Flows Thoroughly
Before you ship a flow that cuts text, check each risk below: what it looks like in a run, its guard, and the value to test it with.
| Risk | What you see | Guard | Test with |
|---|---|---|---|
| Length runs past the end | The run fails with a length error | slice; no length or a min() cap only when every value reaches the start | A value shorter than your length or your start index |
| Character is missing | No error: the whole text, or all but its last character | Check indexOf for -1 first | A value without the character |
| Character repeats or is out of order | No error: a cut at the wrong match, or an empty string | Choose indexOf, nthIndexOf, or lastIndexOf on purpose | The character twice; a closing character first |
| Blank value | An error such as “Expected String but got Null”, or a length error | coalesce(value, '') with slice | A blank value |
Validate Parameters
The value may not be text. String functions work only on strings, Microsoft’s reference says, so convert a number first: string(42) returns '42', as in Microsoft’s cookbook. Test with a number.
Be Cautious with Special Characters
Line breaks need decodeUriComponent. Microsoft’s cookbook warns that \n typed in an expression gives the literal characters \n, not a line break. To cut at a line break, first turn Windows-style breaks into plain ones with replace(<text>, decodeUriComponent('%0D%0A'), decodeUriComponent('%0A')), then search for decodeUriComponent('%0A'). Test with a value that has Windows-style line breaks.
Break Down Complex Tasks
One step per Compose. Find the position in one Compose action and cut the text in the next, so the run shows each value, as Microsoft’s troubleshooting tip suggests.
Conclusion
Which function to reach for:
- substring for fixed positions in text with a fixed shape.
- slice when positions come from indexOf or the length varies.
- split for the nth piece between delimiters.
Do you want to learn more about Power Automate and its possibilities? Take a look at our course Power Automate Training: Learn How to Automate Your Business Processes.
Heads-up for Power Automate makers: Microsoft retired Describe it to design it, the option that builds a flow from a plain-language description, on November 30, 2026. “On November 30, 2026, Describe it to design it in Power Automate retires for all customers.” Existing flows aren’t affected. If an AI assistant now writes your expressions, get a second pair of eyes on one flow: the free 30-minute AI app review.
FAQs
How do you get a substring in Power Automate?
In a cloud flow, use substring(text, startIndex, length). Counting starts at 0, and length is optional: leave it out to get the rest of the text. Microsoft’s example, substring('hello world', 6, 5), returns world, the five characters from position 6 (substring reference).
- In the designer: in a Compose action, select Inputs, select fx, enter the expression, and select Add.
- Not in a cloud flow? Desktop flows use Get subtext; Power Apps uses Mid(), which counts from 1.
What is the difference between substring and slice in Power Automate?
Substring takes a start index and a length. Microsoft’s parameter table asks for a start of 0 or more and a positive length, and substring fails if the two run past the end of the text. Slice takes a start and an end index, accepts negative values that count back from the end, and stops at the end of the text instead of failing (slice reference).
Does Power Automate have Left, Right, and Mid functions?
Not in cloud flow expressions: left(), right(), and mid() aren’t in Microsoft’s string functions list. The expression cookbook builds all three from substring, like the first three recipes above. Power Fx has Left, Mid, and Right, where Mid counts from 1, in canvas apps and desktop flows, among others.
How do I split a string into an array in Power Automate?
Use split with the delimiter: split('a_b_c', '_') returns ["a","b","c"], and text without the delimiter comes back as a one-item array (split reference).
- To keep one piece, wrap the split in first() or last():
last(split('Shared Documents/Finance/report.xlsx', '/'))returnsreport.xlsx. - To cut every item, put the split() in a Select action’s From field and the cut in Map, using
item()for the current piece, for exampleslice(item(), 0, 3)(Microsoft’s Select steps). An Apply to each loop works too. - Spaces after the delimiter stay in each item, so Microsoft’s cookbook suggests running trim() on each item in a Select action afterward.
How do you strip characters from a string in Power Automate?
Replace them with an empty string: replace(variables('text'), '-', '') removes every hyphen.
- replace() is case-sensitive and changes every match, as in the cookbook’s
replace('2026/03/18', '/', '-'), which returns2026-03-18(cookbook). - Use trim() for spaces at the start and end.
- To strip by position,
slice(variables('text'), 3)drops the first three characters, andslice(variables('text'), 0, -3)drops the last three.
How do you extract text from a string in Power Automate?
Find the position first, then cut.
- indexOf, lastIndexOf, and nthIndexOf return where a piece of text starts. indexOf and nthIndexOf return -1 when it’s missing, so check for -1 first (the guard in the tradeoffs section), then pass the number to slice.
- Expressions nest, so one function’s result feeds the next:
slice(variables('text'), add(indexOf(variables('text'), '@'), 1))turnsalice@contoso.comintocontoso.com. - The recipes above cover the other common cuts.
Changelog: November 2026, refreshed for the new flow designer. Text steps replace the classic designer screenshots, the copy-paste recipes gained guards for missing characters and blank values, the string function table now lists cloud flow functions (the previous table listed Power Automate Process Mining operations, which Microsoft says aren’t available in other parts of Power Automate), and the FAQ has its answers back. Results on this page follow Microsoft’s function definitions; the error capture is from a 2023 classic-designer run.
Related articles
Power Automate Functions: Cheat Sheet and Best Practices
Power Automate functions cheat sheet: expression functions by category with examples, the div() integer-division gotcha, and best practices.
Power Automate formatDateTime: Format Codes and Examples
Power Automate formatDateTime: format codes in one table, plus date only, AM/PM, 24-hour, ISO 8601, and time zone examples. Updated November 2026.
Power Automate Compose: What It Does and When to Use It
The Power Automate Compose action holds one value or expression output for reuse. What it does, Compose vs variables, a worked example, and debugging tips.