← Al Davies, 250 High Street

How to count the characters in text in Power Automate

Use the length() expression with your text inside it, such as length(triggerOutputs()?['body/Title']). The flow returns the number of characters as a whole number.

What is the expression to get the length of text?

Add a Compose action. On the Expression tab, enter length('Hello world') and select OK. The result is 11.

To count text from a column, replace the quoted text with the dynamic content for that column. For example: length(triggerOutputs()?['body/Title']).

Does length() count spaces?

Yes. Every character counts, including spaces, tabs and line breaks. The text 'a b' has a length of 3.

If stray spaces change your count, remove them first. Use length(trim(variables('Name'))). The guide on trimming spaces from text shows how.

How do you check if text is empty?

Compare the length to zero in a Condition: length(variables('Name')) is equal to 0. An empty text value has a length of 0.

The empty() function does the same check in one step. For more ways to test blank fields, see checking if a field is empty.

How do you stop a flow when text is too long?

Add a Condition with the expression length(triggerOutputs()?['body/Title']) is greater than 255. Send the Yes branch to a step that shortens the text or alerts the owner.

This helps when a target field has a limit. SharePoint, for example, limits hyperlink columns to 255 characters. See the hyperlink 255 character limit guide.

How do you cut text to a set length?

Use substring() with the smaller of the length and your limit. Example: substring(variables('Text'), 0, min(length(variables('Text')), 100)).

The min() part matters. substring() fails when the count you ask for is longer than the text. Read extracting text with substring for the rules.

Why does length() fail with a null value?

If the column is empty, the value can be null, not empty text. Then length() returns an error because it cannot measure a missing value.

Wrap the value with coalesce(): length(coalesce(triggerOutputs()?['body/Title'], '')). This turns null into empty text and returns 0.

How is this different from counting items in an array?

The same length() function works on both. For text, it counts characters. For an array, it counts items.

To count the rows that Get items returns, see counting items in an array. To split text into an array first, use split and then take the length.

FAQ

Does length() count words?

No. It counts characters. To count words, split the text on spaces and take the length of the array: length(split(variables('Text'), ' ')).

Does an emoji count as one character?

Not always. Some emoji use two UTF-16 code units, so length() can return 2 for one emoji. Test with your own data if this matters.

Can I use length() on a number?

Not directly. Convert the number to text first with string(). For example, length(string(12345)) returns 5.

Sources