The Power Automate Slice function lets you extract a substring of a given string by defining a start and end index within a given string.
The slice function is very similar to the substring function, which does basically the same by providing a start index and a length of the needed substring.
In this article, I’ll introduce you to the slice function and will provide a step-by-step guide on how to use the function in a Microsoft flow.
Power Automate Slice Function
Extract a portion of a string by specifying the starting and ending point or value.
slice('<text>', <startIndex>, <endIndex>?)
- text (mandatory): The input string that holds the substring to be searched for.
- startIndex (mandatory): The input is an integer that represents the starting point for searching for the substring. If the provided index is greater than the length of the string, an empty string will be returned. If the index is negative, the search will begin at the calculated position by adding the length of the string with the provided negative index.
- endIndex (optional):: The input is an integer that represents the ending point for searching for the substring. The character at this index will not be included in the search. If the provided index is not specified or greater than the length of the string, the search will go up to the end of the string. If the index is negative, the search will end at the calculated position by adding the length of the string with the provided negative index.
- Retrieve a new string containing the identified substring.
Power Automate Slice Expression Example
Let’s imagine we have the following string, and we want to slice ‘John’ out of the string.
"I am John Doe."
The index starts at 0 the J is at position 5 and the endIndex is the first character after the searched string, so it must be the 9.
The Power Automate slice expression must look like this, to retrieve the “John” slice.
slice('I am John Doe.',5,9) => "John"
How To Use The Power Automate Slice Function
Now that we have a basic understanding of the function. Let’s take a look at the slice function in flow.
Create a new flow with trigger ‘Manually trigger a flow’
Add a new step by clicking on ‘+ New step’
Add a new step
Search for ‘initialize’ and click on ‘Initialize variable’ action
Add Initialize variable action
Configure Initialize variable action: 1) Provide a name,2) select ‘string’, 3) Set the value to ‘I am John Doe.’
Initialize variable action
Add a Compose action step by searching for ‘compose’ and clicking on the ‘Compose’ action
Add a compose action
Configure compose: 1) Click into Inputs text field (popup appears), 2) click on ‘Expression’, 3) set expression to ‘slice(variables(‘myText’),5,9)’, 4) click ‘OK’
Power Automate slice expression example
Save and do a test run
Result Test run – Power Automate slice function