# Power Automate substring(): Get Part of a Text Value

> For the full documentation index, see https://250highstreet.com/llms.txt

> Use substring() in Power Automate to cut part of a text value. Start index, length, errors on short text, and how to get the first or last characters.

Use the substring() expression. Write substring('Hello World', 6, 5) to get 'World'. The first number is the start position, counted from 0, and the second is the length.

## What does the substring() function do?

The substring() function returns characters from a string. Microsoft documents it as substring('\<text\>', \<startIndex\>, \<length\>). The start index is zero-based, so the first character is at position 0.

The length is optional in the documentation. If you leave it out, the function returns all characters from the start position to the end of the text.

## How do you take the first characters of a text value?

1.  Add a Compose action, or select the field where you need the result.
2.  Open the Expression tab.
3.  Type substring(triggerOutputs()?\['body/Title'\], 0, 5), with your own column name in place of Title.
4.  Select Add. The result holds the first 5 characters.
5.  Save and run the flow. Check the Compose output in run history.

## Why does substring() fail on short text?

The start position plus the length must not go past the end of the text. If the text is 3 characters long and you ask for 5, the action fails with an error that the parameters are out of range.

Check the length first. Write if(greater(length(variables('Code')), 4), substring(variables('Code'), 0, 5), variables('Code')). The flow then returns the whole text when it is too short.

A null input causes a different error. Read [the invalid type error about null](/guides/power-automate-invalid-type-expected-string-but-got-null/) and guard the input with coalesce().

## How do you get the last characters of a text value?

Count back from the end. To get the last 4 characters, write substring(variables('Code'), sub(length(variables('Code')), 4), 4). The sub() function finds the start position and length() gives the total size.

The text must hold at least 4 characters. Otherwise the start position is below 0 and the action fails.

## How do you cut text before or after a character?

Combine substring() with indexOf(). The indexOf() function returns the position of a search text. Microsoft documents it as case-insensitive. It returns -1 when the text is not found.

For the part before an at sign, write substring(variables('Email'), 0, indexOf(variables('Email'), '@')). For the part after it, write substring(variables('Email'), add(indexOf(variables('Email'), '@'), 1)).

If you only need to break text at a separator, [split the string](/guides/power-automate-split-string/) instead. It needs no position math.

## What other functions cut text?

The slice() function takes a start and an end position instead of a length. The take() function returns the first characters. Microsoft documents both in the expression reference.

To test whether text holds a word before you cut it, use [the contains() function](/guides/power-automate-contains-function-text-array/). To skip empty values, see [how to test for an empty field](/guides/power-automate-condition-field-empty/). Test with your own data and check the exact output in run history.

## FAQ

### Does substring() start at 0 or 1 in Power Automate?

It starts at 0. The first character is at position 0.

### What happens if the length is too long?

The action fails with an out-of-range error. Check length() first and return the whole text when it is short.

### Is indexOf() case-sensitive?

No. Microsoft documents indexOf() as case-insensitive. It returns -1 when the text is not found.

### How do I get the text before a character?

Use substring(text, 0, indexOf(text, 'x')), with your own character in place of x.

## Sources

- [Reference guide to workflow expression functions (Microsoft Learn)](https://learn.microsoft.com/en-us/azure/logic-apps/workflow-definition-language-functions-reference)

## Related guides

- [Split text into parts in Power Automate](/guides/power-automate-split-string/)
- [Power Automate contains(): Check Text or a List for a Value](/guides/power-automate-contains-function-text-array/)

Source: https://250highstreet.com/guides/power-automate-substring-extract-text/
