Learn how to use Power Automate substring function to extract text, clean data, and optimize workflows. Step-by-step examples, tips, and best practices included.
Contact UsHave you ever struggled with extracting specific parts of text in your Power Automate flows? Maybe you need to grab the first few characters from an email subject, pull out a reference number from a long string, or clean up messy data before processing it.
You're not alone. Text manipulation is one of the most common challenges in workflow automation, and that's exactly where the substring function becomes your best friend.
In this comprehensive guide, you'll learn everything about using substrings in Power Automate - from basic extraction to advanced techniques that'll save you hours of manual work. Whether you're building your first flow or optimizing existing workflows, this article will show you practical solutions that actually work.
Let's dive in and transform how you handle text in your automation workflows.
The substring function extracts a portion of text from a larger string. Think of it like using scissors to cut out exactly the part you need from a piece of paper - no more, no less.
substring(text, startIndex, length)
Let's say you have an order number like "ORD-2025-12345" and you only need the year "2025".
substring('ORD-2025-12345', 4, 4)
Why it works:
Scenario: You receive customer emails and need to identify which company they're from.
The Challenge: Email addresses like "johndoe@techimplement.com" - you need just "techimplement.com"
Solution:
substring(
variables('email'),
add(indexOf(variables('email'), '@'), 1),
sub(length(variables('email')),
add(indexOf(variables('email'), '@'), 1))
)
What's happening here:
Scenario
Your invoices follow the format "INV-2025-MARCH-001" but you only need the month.
Solution
substring('INV-2025-MARCH-001', 9, 5)
Pro Tip: For variable positions, combine with indexOf to find the dash positions dynamically.
Scenario
Phone numbers come in as "(555) 123-4567" but your system at TechImplement needs "5551234567"
Solution with Function Combination
replace(replace(replace(variables('phone'), '(', ''), ')', ''), '-', '')
Then extract specific parts
substring(variables('cleanPhone'), 0, 3) // Area code
Scenario
Date string "2025-12-15" needs to be split into year, month, and day.
Complete Solution
Year: substring('2025-12-15', 0, 4)
Month: substring('2025-12-15', 5, 2)
Day: substring('2025-12-15', 8, 2)
Scenario
Reference codes like "CUST-GOLD-2025-A123" need category extraction.
Solution
Category: substring('CUST-GOLD-2025-A123', 5, 4)
Year: substring('CUST-GOLD-2025-A123', 10, 4)
Code: substring('CUST-GOLD-2025-A123', 15, 4)
Beyond substring, Power Automate provides a complete toolkit of string operations for text manipulation. Here's your comprehensive reference guide to all available string functions:
SUBSTRING([string],[start],[count])
LEFT([string],[count])
RIGHT([string],[count])
CONTAINS([string],[value])
STARTSWITH([string],[value])
ENDSWITH([string],[value])
UPPER([string])
LOWER([string])
TRIM([string])
LTRIM([string])
RTRIM([string])
LEN([string])
CONCAT([string1],...,[stringN])
TOINT([string],[default])
TOSTRING Functions
TOSTRING([int],[format]*)
TOSTRING([float],[format]*)
TOSTRING([bool])
TOSTRING([date],[format]*)
TOSTRING([time],[format]*)
When you don't know exact positions, calculate them on the fly:
substring(
variables('text'),
add(indexOf(variables('text'), '['), 1),
sub(
indexOf(variables('text'), ']'),
add(indexOf(variables('text'), '['), 1)
)
)
This grabs everything between square brackets, no matter where they are.
Getting the last 4 digits of a credit card:
substring(
variables('cardNumber'),
sub(length(variables('cardNumber')), 4),
4
)
Grabbing text between two delimiters:
substring(
variables('text'),
add(indexOf(variables('text'), 'START'), 5),
sub(
indexOf(variables('text'), 'END'),
add(indexOf(variables('text'), 'START'), 5)
)
)
Extract different portions based on conditions:
if(
greater(length(variables('text')), 10),
substring(variables('text'), 0, 10),
variables('text')
)
Error 1: Index Out of Range
Problem
substring('Hello', 10, 5) // Fails - starts beyond string length
Solution
if(
less(variables('startPos'),
length(variables('text'))),
substring(variables('text'),
variables('startPos'),
variables('len')),
''
)
Solution with validation
if(
and(
not(empty(variables('text'))),
less(variables('startPos'), length(variables('text')))
),
substring(
variables('text'),
variables('startPos'),
min(variables('len'), sub(length(variables('text')), variables('startPos')))
),
''
)
Error 2: Null or Empty Strings
Problem
substring(variables('emptyText'), 0, 5) // Fails on null
Solution with comprehensive checks
if(
and(
not(empty(variables('text'))),
greater(length(variables('text')), 0),
greater(length(variables('text')),
variables('requiredLength'))
),
substring(variables('text'), 0,
variables('requiredLength')),
'DEFAULT_VALUE'
)
Complete Validation Pattern
if(
and(
not(empty(variables('input'))),
greater(length(variables('input')), variables('requiredLength')),
contains(variables('input'), variables('delimiter'))
),
substring(variables('input'), variables('start'), variables('length')),
'ERROR: Invalid input format.'
)
Extract the domain from the email, then get the company name:
first(split(
substring(
variables('email'),
add(indexOf(variables('email'), '@'), 1),
sub(length(variables('email')),
add(indexOf(variables('email'), '@'), 1)
),
'.'
))
Clean and extract:
trim(replace(substring(variables('text'), 0, 10), '-', ''))
Standardize extracted text:
toLower(substring(variables('code'), 0, 5))
Problem
Extracted text doesn't match expectations
Solution
Problem
Works sometimes, fails other times.
Solution
Problem
Extraction fails with special characters.
Solution
Problem
Flow takes too long with substring operations
Solution
Check if text exists before extracting:
if(
greater(length(variables('text')), 10),
substring(variables('text'), 0, 10),
variables('text')
)
Bad
substring(variables('x'), 0, 5)
Good
substring(variables('customerEmail'), 0, 5)
Inefficient
substring(variables('text'), indexOf(variables('text'), '@'), 5)
substring(variables('text'), indexOf(variables('text'), '@'), 3)
Optimized
Set variable 'atPosition' = indexOf(variables('text'), '@')
substring(variables('text'), variables('atPosition'), 5)
Instead of
substring(trim(toLower(replace(variables('text'), '-', ''))), 0, 5)
Break it down
Step 1: replace(variables('text'), '-', '')
Step 2: toLower(variables('step1'))
Step 3: trim(variables('step2'))
Step 4: substring(variables('step3'), 0, 5)
Mastering substring and string operations in Power Automate is essential for effective text manipulation in your workflows. By understanding when to use each function and following best practices for validation and error handling, you'll build robust automation that handles any text processing challenge efficiently.
Start implementing these techniques today and transform your text processing workflows!
Yes, substring works perfectly with dynamic content from triggers and previous actions like email subjects, SharePoint items, or HTTP responses. Simply select the dynamic content in your flow where you'd normally use a variable, making it flexible for real-time data processing.
Power Automate throws an error for negative start positions, so always validate calculated values with conditions first. Zero-length values return empty strings without failing, but it's best practice to ensure your calculations always produce positive numbers.
Power Automate handles strings up to 100MB, but processing very large strings over 1MB can cause performance slowdowns and timeouts. For large text files, split the content into smaller chunks first or consider using Azure Functions for better performance.
Chaining multiple substring operations is inefficient because it recalculates positions repeatedly. Instead, calculate all positions once and store them as variables, then reference those variables in separate substring calls for better performance.
Use indexOf for the first occurrence and lastIndexOf for the last occurrence of any delimiter. For multiple occurrences in between, use the split function to create an array and access specific segments with array indexing.
Substring is best for position-based extraction when you know exact character positions or counts. Split is ideal for delimiter-separated data where you need to extract segments between consistent separators like commas, dashes, or pipes.
ISO-certified processes ensuring clean, scalable, and maintainable code on every project.
Unlock unbeatable value with our competitive rates and cost-effective solutions.
Stay informed every step of the way with our transparent processes.
© 2025 All Rights Reserved By TechImplement