Hello everyone and welcome to my blog. In today’s blog I will discuss the following requirements.
- Return a substring by specifying the start and end position value
- Return the LastN characters from a string
- Return the all characters from a string except the last N characters.
- Return FirstN characters from a string in Power Automate
Through my blog I share interesting tips and discuss on latest releases of Microsoft.NET technologies, Dynamics 365 and Power Platform, SharePoint and Client scripting libraries. Please subscribe to my blog to stay updated.
So let’s get started. I am going to use the slice function for all these requirements. Before we go ahead and jump into the examples, let’s understand the nomenclature of the slice function.
The slice function has three parameters.
slice('<text>', <startIndex>, <endIndex>?)
- text – the input string
- startIndex – the starting index of search. Starts with zero based index.
- endIndex – the end index of search. It’s an optional parameter.
Let’s start with the simplest of examples first. Let’s find out the substring from the 3rd to the 10th position of the text input. Below is the expression to accomplish this.
slice('abcdefghijklmnopqrts', 2, 9)
And the output of the above expression will be – cdefghij
That’s the easy one. How about the retrieving the Last N characters from a string. For example – say the requirement is to retrieve the last 4 characters from the above text input.
You can easily do that using the below expression.
slice('<text input>', -4)
Below is the output when I run the flow. Observe the usage of negative value for the startIndex parameter.
How about a more complex requirement? Let’s say you need to retrieve all characters of a string from the start of the string except the last 4 characters.
As simple as it might sound, it’s not so straightforward. Especially when you don’t know the length of the string. The below expression use slice function to make this operation pretty simple.
Observe the usage of the negative value for endIndex parameter.
slice('<text input>', 0, -4)
Hope this helped. To stay updated with similar interesting topics, please subscribe to my blog.
Debajit Dutta
Business Solutions MVP
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.