DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday – Slice and Split

This week I’m returning to some of the string manipulation functions: slice and split.

slice

The slice function is essentially an enhanced version of the substring function. The pattern for the function is as follows:

slice(string, startIndex, endIndex)
Enter fullscreen mode Exit fullscreen mode

The first parameter is the string to be examined. As always this can be a string literal, a variable, or the output from a previous trigger or action.

The second is the startIndex. Unlike the substring function, if the startIndex value is greater than the length of the string, an empty string is returned instead of an error. You can also pass in a negative value for your startIndex. Doing so will count backward from the end of the string and start looking there. For example:

slice('ABCDEFGHIJKLMNOP', -5) //will return 'LMNOP'
Enter fullscreen mode Exit fullscreen mode

The first two parameters are required.

The last parameter, endIndex, is optional. Instead of being the length of string to return, it will instead return the string from the startIndex position to the endIndex position. For example:

slice('ABCDEFGHIJKLMNOP', 2, 5) //will return 'CDE'
Enter fullscreen mode Exit fullscreen mode

When the endIndex is not provided, the result will be from the startIndex to the end of the string.

slice('ABCDEFGHIJKLMNOP', 10) //will return 'KLMNOP'
Enter fullscreen mode Exit fullscreen mode

The endIndex can also be a negative value. Like the startIndex, when endIndex is negative, it will count backward from the end of the string to determine the end position. Example:

slice('ABCDEFGHIJKLMNOP', 10, -2) //will return 'KLMN'
Enter fullscreen mode Exit fullscreen mode

Whether you use substring or slice depends on your use case and how you need to determine what portion of the string you need to extract. There are advantages to each. If you need to cut based on positions, you generally use slice. If you need to cut based on length, then that’s where substring has the advantage.

split

The split function is used for breaking a string apart into an array of substrings. The most common use case is when you have a comma, or otherwise, delimited list passed in as a string and you need to break it apart into its distinct elements so that you can process each element. The pattern is as follows:

split(stringToSplit, delimiter)
Enter fullscreen mode Exit fullscreen mode

Obviously, the first parameter is the string you want to split up: string literal, variable, or output from a previous action or trigger.

The second parameter is the delimiter. This will be whatever character or characters you are using to split your string up. It’s important to remember that the delimiter character will not be included in the output.

Examples:

split('A,B,C,D,E', ',') //returns ["A", "B", "C", "D", "E"]
split('Rubber baby buggy bumpers', 'b')  
//returns ["Ru", "", "er ", "a", "y ", "uggy ", "umpers"]
split('There the thing threw a thousand threads', 'th')
//returns ["ere ", "e ", "ing ", "rew a ", "ousand ", "reads"]
Enter fullscreen mode Exit fullscreen mode

And there you go. Two more string functions to make your Power Automate flows more useful.

The post Function Friday – Slice and Split first appeared on Barret Codes.

Top comments (0)