String operations using index, FirstN, LastN characters from string in Power Automate

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

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>?)
  1. text – the input string
  2. startIndex – the starting index of search. Starts with zero based index.
  3. 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.

Discover more from Debajit's Power Apps & Dynamics 365 Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading