Power Automate has quite a few string functions that can help you sort out textual issue. Two of these functions are Substring and Slice. Do you know the difference?

Substring

Substring returns characters from a string, starting from the specified position, or index. Index values start with the number 0.

Examples:

substring('This_is_a_substring', 10)

The above example will result in ‘substring’ being returned. As we have only given the start index(10) we will get all characters from the 11th character. Notice that the first character is 0 and not 1.

substring('This_is_a_substring', 10, 5)

This will return ‘subst’ as we are asking for the first 5 characters after the 10th character as the starting index is 10, which is the 11th character.

Screenshot of a user interface for manually triggering a flow, featuring options for 'Substring' and 'Slice' with expressions displayed in an expression box.
Substring vs Slice in Power Automate 1

Slice

Slice, returns a substring by specifying the starting and ending position or value.

Slice('This_is_a_slice', 10)

There is no difference between the first syntax of Substring and the first syntax of Slice.

Slice('This_is_a_slice', 10, 14)

This example will return ‘slic’. as we take the characters between 11th and 15th not including the 15th character.

I’ll make that last example a little easier. The followig example give us just a h.

Slice('This_is_a_slice', 1, 2)

Should we use Substring or Slice?

A lot here depends on what you’ve got. In both cases you will need to know the starting point of your string. Now depending on if you know how many characters you want or if you have the number of characters your original text had you may pick on or the other.

The following example is never a good idea, where we calculate our end or the string.

slice('This_is_a_substring', 10, sub(length('This_is_a_substring'), 5) )

or

substring('This_is_a_substring', 10, sub(length('This_is_a_substring'), length('This_is_a_')))

This is not really a performance difference but more a simplification of flows that will make it easier to understand what the flow is doing.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.