The trim() function in Power Automate removes leading and trailing whitespace (spaces, tabs, and newlines) from a string. Use it in expressions like trim(triggerBody()['text']) to clean user input before processing.
What Is the trim() Function?
Power Automate (formerly Microsoft Flow) provides a set of string functions that work inside expressions. The trim() function takes a single string argument and returns a new string with whitespace removed from both the beginning and the end. It does not remove spaces inside the string—only those at the edges. This is useful when you receive data from forms, emails, or external systems where users may accidentally add extra spaces.
For example, if a SharePoint column contains " John Doe ", trim() will return "John Doe". The function is case-sensitive and works with any string data type, including dynamic content from triggers and actions.
How to Use trim() in an Expression
You can insert trim() anywhere an expression is allowed in Power Automate. The most common places are:
- In a Compose action to clean a value before using it later.
- In a Condition to compare trimmed values (e.g., avoid false negatives due to extra spaces).
- In a Apply to each loop when processing items from a list or array.
- In a Send an email action to display a clean subject or body.
To use it, open the dynamic content panel, switch to the Expression tab, type trim(), and place your string or dynamic content inside the parentheses. For instance:
trim(triggerBody()['text'])— trims the ‘text’ field from the trigger.trim(variables('myVar'))— trims a variable.trim(item()?['Name'])— trims a field from the current item in a loop.
If the input is not a string, Power Automate will try to convert it. For numbers and booleans, it returns the string representation trimmed (which usually has no spaces).
Common Use Cases and Examples
Here are three practical scenarios where trim() solves real problems:
- Cleaning form responses: Microsoft Forms often includes extra spaces from mobile users. Before storing in SharePoint, use
trim()in a Compose action:trim(formResponse('Email')). - Comparing user input to a known value: If you check if a user’s answer equals “Yes”, a leading space would cause the condition to fail. Use
equals(trim(outputs('Get_response')?['body/r1']), 'Yes'). - Building file names: When creating a file name from a title, trim it first to avoid unwanted spaces in the file name:
concat(trim(item()?['Title']), '.txt').
In each case, trim() ensures your data is consistent and avoids errors in downstream actions.
How to Choose Between trim(), replace(), and Other Functions
While trim() removes only leading and trailing whitespace, you might need other functions for different cleaning tasks:
replace()— removes all occurrences of a specific substring, including spaces inside the string. For example,replace('a b c', ' ', '')returnsabc.substring()— extracts a portion of a string, useful if you need to remove a fixed prefix or suffix.concat()— combines strings, often used withtrim()to build clean outputs.
Use trim() when you only need to remove whitespace at the edges. If you need to remove all spaces (including internal ones), use replace(). If you need to remove a specific character like a comma, use replace() with that character.
Common Problems and Misconceptions
One frequent mistake is assuming trim() removes all whitespace. It does not. Another is using trim() on a null value, which causes an error. To avoid that, use coalesce() to provide a default: trim(coalesce(triggerBody()?['text'], '')).
Another misconception is that trim() modifies the original string. In Power Automate, strings are immutable—the function returns a new string, so you must assign the result to a variable or use it directly in an expression. If you need to permanently clean a field, you must update the source (e.g., update a SharePoint item) or store the trimmed value in a new variable.
Also note that trim() removes Unicode whitespace characters, not just the space character (U+0020). This includes tab (U+0009) and newline (U+000A). However, it does not remove non-breaking spaces (U+00A0) in some cases; if you encounter that, use replace() with the specific character.
Comparing trim() Across Power Automate, Excel, and Other Tools
If you are familiar with Excel, the TRIM function in Excel removes extra spaces between words as well, leaving only single spaces. In Power Automate, trim() does not collapse internal multiple spaces—it only removes edges. For example, trim(' Hello World ') returns 'Hello World' (still with triple spaces inside). If you need to collapse internal spaces, you would need a more complex expression using replace() and a loop, or use the split() and join() trick: join(filter(split(input, ' '), @not(equals(item(), ''))), ' ').
In Power Query (used in Excel and Power BI), the Text.Trim function behaves similarly to Power Automate’s trim(). In programming languages like JavaScript, trim() also removes only leading and trailing whitespace. So Power Automate’s behavior is consistent with most languages, but differs from Excel’s TRIM.
Performance and Cost Considerations
Using trim() has negligible performance impact. However, be mindful of how many times you call it inside loops. If you have a large array (e.g., thousands of items), trimming each item inside an Apply to each loop can add processing time. In such cases, consider trimming the entire input once if possible, or use a Select action to transform the array in one step.
Regarding cost, Power Automate licensing is based on flows and actions, not on function usage. Using trim() does not consume extra actions—it is part of the expression evaluation. So there is no additional cost for using this function.
Frequently Asked Questions
Does trim() remove spaces inside the string?
No. It only removes leading and trailing whitespace. Spaces between characters remain unchanged. To remove all spaces, use replace(string, ' ', '').
Can I use trim() on a number or a boolean?
Yes, but Power Automate converts the value to a string first. For example, trim(123) returns '123' (as a string). This is rarely needed because numbers don’t have spaces, but it works.
What happens if the input is null?
It throws an error. To handle null, use trim(coalesce(yourValue, '')) to default to an empty string.
Does trim() remove newline characters?
Yes, it removes all Unicode whitespace at the edges, including newline () and carriage return (
). This is useful when cleaning multiline text.
How do I trim a field from a SharePoint item?
Use trim(item()?['FieldName']) inside an Apply to each loop, or trim(outputs('Get_item')?['body/FieldName']) if you have a single item.
Related
- Google Finance Dividend Yield: How to Find It
- Snowflake to Number: Convert with TO_NUMBER & CAST
- Python dict update method: how it works
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Using NPV in Excel: Formula, Examples, and Timing GuideTo calculate net present value in Excel, use the NPV function for future cash flows, then add the…
- Excel Calendar with One Formula? Yes, Here’s HowYes, you can create a dynamic calendar in Excel with a single formula using the SEQUENCE function combined…
- How to Multiply Hours in Excel: Formulas and FormattingTo multiply a duration by a factor in Excel, enter the duration in A2 and the factor in…
- Excel OFFSET Function Dynamic Range ExplainedThe Excel OFFSET function creates a dynamic range by returning a reference to a cell or range that…
- Excel EDATE Function: Add or Subtract Months from a DateUse Excel’s EDATE function to move a date forward or backward by a specified number of whole months:…
- Excel Formula for Percentage Increase: Examples and TipsTo calculate percentage increase in Excel, subtract the old value from the new value, then divide by the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.