DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday – Concat & Substring

For the first of my #FunctionFriday posts I’m going to focus on a couple of the most common string functions: concat and substring.

concat

The first function we’ll look at is concat. This function lets you glue together 2 or more other strings into a single string. The syntax is simple:

concat(string 1, string 2, string 3)
Enter fullscreen mode Exit fullscreen mode

There isn’t a limit on the number of strings you can combine together, but you have to supply at least 2 arguments. These arguments can be any combination of literal strings, string variables, and string outputs from previous actions in the flow.

Let’s look at a quick example. We have a variable var1 with a value of “Hello” and a button trigger that asks for a person’s name. We want to generate an output that states “Hello, , welcome to Power Automate”. We would write our expression function as follows:

concat(variables('var1'), ', ', triggerBody()['text'], ', welcome to Power Automate')
Enter fullscreen mode Exit fullscreen mode

We concatenate our var1 variable, then a string literal with our comma punctuation, followed by the name field from our button trigger, and finish off with another string literal that contains the rest of our sentence.

The only limitation on the concat function is that the return value cannot exceed 104,857,600 characters. Nothing significant. 🙂

substring

The other function we’ll look at today is the substring function. You use this to grab a portion of an existing string. That string can be a string literal, a string variable, or an output from a previous step in your flow. The pattern of the function is as follows:

substring(textToSearch, startIndex, length)
Enter fullscreen mode Exit fullscreen mode

The first argument is the source string you want to pull a substring out of. The second argument is an integer value indicating where to start. This value is 0 based. So if you want to start at the beginning of the string, you pass in 0, not 1. The last argument is an integer value indicating how many characters to include.

This function has a couple of gotchas around the 2nd and 3rd arguments. The values you include must be within the total length of the string or the step will fail your flow with an error message.

Take, for example, the following string: “Hello there, Bill”. This string is 17 characters in length. So, if your start position or the total of start position + length are 17 or greater (remember this is 0 based), then your flow will fail.

substring('Hello there, Bill', 0, 5)
Enter fullscreen mode Exit fullscreen mode

will return “Hello”.

substring('Hello there, Bill', 12, 6)
Enter fullscreen mode Exit fullscreen mode

will throw an error.

As such, when using substring you will need to put in place various checks to ensure that your start position and length will not take you beyond the length of the string you are searching.

The post Function Friday – Concat & Substring first appeared on Barret Codes.

Top comments (0)