Substring Function in Power Automate with Real-Time Examples​

Substring Function in Power Automate: 10 Real-Time Examples

No comments

Loading

In this “Substring Function in Power Automate” article, we will learn about the substring function in Power Automate and how it works in Power Automate flows with some real-time project scenario-based examples. Power Automate, a powerful automation tool by Microsoft, equips users with various functions to manipulate data and create efficient workflows. One such crucial function is substring, allowing users to extract a portion of a string based on a defined starting index and length. In this article, we will dive into the substring function and provide numerous real-time examples to showcase its utility and flexibility.

Understanding the Substring Function

The substring function in Power Automate allows users to extract a specified portion of a string based on a defined starting index and length.

Syntax of Substring Function in Power Automate:

substring(expression, start, length)
Syntax of substring Functions in Power Automate
Syntax of substring Functions in Power Automate

Parameters:

  • Expression (String): The input string from which the substring will be extracted.
  • Start (Int): The starting index from which the substring will begin.
  • Length (Int): The length of the substring to be extracted.

Substring Function in Power Automate: Real-time Examples

We will demonstrate the usage of the substring function in various scenarios. Each example follows the format: first, the syntax of the function expression, then the actual example with passing values, and finally the output of the function.

Example: Extracting a Substring from a Specific Index

This example extracts a substring from a specific index.

Example:

Expression: substring('Power Automate is powerful.', 6, 8)

Output:

Automate

Example: Extracting a Substring with Maximum Length

This example extracts a substring with the maximum length.

Example:

Expression: substring('This is a sample string.', 0, 24)

Output:

This is a sample string.

Example: Extracting a Substring from a SharePoint List Item

This example extracts a substring from the SharePoint List item Title column; it uses the for each loop of Power Automate(apply to each control in Power Automate).

Example:

Expression: substring(items('Apply_to_each')['Title'], 2, 5)

Output:

It depends on the specific item's title.

Example: Extracting a Substring from a CSV String

This example extracts a substring from a CSV string.

Example:

Expression: substring('value1,value2,value3,value4', 14, 6)

Output:

value3

Example: Extracting a Substring from a JSON Object in Power Automate

This example extracts a substring from a JSON object.

Example:

substring(body(‘Parse_JSON’)?[‘property’], 2, 4)

Output:

It depends on the specific property in the JSON object.

Let’s assume this is my JSON object.

{
"name": "John Doe",
"email": "john.doe@example.com"
}

From this JSON object, I want to extract the substring @example.com from the email property, and then we can use the below expression:

Expression: substring(body('Parse_JSON')?['email'], indexof(body('Parse_JSON')?['email'],'@'))

Here is the flow part:

Extracting a substring from a JSON Object using Power Automate Flow
Extracting a substring from a JSON Object using Power Automate Flow

Flow Output:

@example.com
Extracting a substring from a JSON Object using Power Automate Flow Output
Extracting a substring from a JSON Object using Power Automate Flow Output

Flow Explanation:

In this demo, first we have to create an object type variable and initialize values; then, we have to parse the JSON object, where we have created the JSON schema for this object. Then, inside the substring function using the Power Automate indexof() function, we got the position of the “@” symbol from the email property of the parsed JSON object, and then we got the desired output.

Get substring from an Array in Power Automate Example

To get a substring from an array in Power Automate, let’s follow the below steps:

Step 1: Create an Array variable

  • Name: Variable name text
  • Type: Array

Value in the array:

[
"Apple", 
"Banana", 
"Cherry", 
"Date"
]

Create an Array variable in Power Automate

Create an Array variable in Power AutomateStep 2: Add an apply to each control (for each loop)

Add an apply each control.

Pass “varArrayDemo” into this box “Select an output from previous steps”.

Get substring from an array in power automate example demo
Get substring from an array in power automate example demo

Add compose data operation action inside the apply to each control section.

In the inputs box, write the below substring expression:

Expression: substring(item(), 0, 3)

Output:

This will extract the first 3 characters from each string in the array. So, the output will be as below:

"App", "Ban", "Che", "Dat"
Extract substring from an array in power automate example demo
Extract substring from an array in power automate example demo

Example: Extract substring from URL in Power Automate

This example is very realistic and needed for flow development while we work with dynamic URLs from SharePoint Online or from any data sources. Let’s look at the below SharePoint Online document URL:

https://abcde.sharepoint.com/:w:/r/_layouts/15/Doc.aspx?sourcedoc=%7B337EFCE1-08BE-44E1-B382-B0600846A30E%7D&file=Document1.docx&action=default&mobileredirect=true

From this URL, if we want to extract the substring after the “?“, then we can use the below expression:

Expression: substring('https://abcde.sharepoint.com/:w:/r/_layouts/15/Doc.aspx?sourcedoc=%7B337EFCE1-08BE-44E1-B382-B0600846A30E%7D&file=Document1.docx&action=default&mobileredirect=true',add(lastindexof('https://ztncx.sharepoint.com/:w:/r/_layouts/15/Doc.aspx?sourcedoc=%7B337EFCE1-08BE-44E1-B382-B0600846A30E%7D&file=Document1.docx&action=default&mobileredirect=true','?'),1))

Output:

sourcedoc=%7B337EFCE1-08BE-44E1-B382-B0600846A30E%7D&file=Document1.docx&action=default&mobileredirect=true

Flow configuration:

Extract substring from an URL in Power Automate
Extract substring from an URL in Power Automate

Flow run result:

Extract substring from an URL in Power Automate demo
Extract substring from an URL in Power Automate demo

Explanation:

  • In this substring() function, we have used the lastIndexOf() function and Add() function of Power Automate. We have used the lastIndexOf() function to get the position of “?“; here we can use the indexOf() function  and nthIndexOf function as well, as in this text, the occurrence of “?” is only once. If we don’t use the Add() function, the substring will start with the question mark (“?“), but we need the substring after the question mark, so we have used the Add() function to start the index after the question mark (“?“).

The ’start index’ for function ‘substring’ must be equal to or greater than zero and must be less than the length of the string.

Note:

Summary: Substring function in Power Automate

Thus, in this article, we have learned about substring functions in Power Automate with real-time examples. This article offers a comprehensive exploration of substring functions, showcasing their application and utility within Power Automate. While direct extraction from a negative index is not supported, the article presents effective workarounds to achieve this.

By demonstrating various substring extraction scenarios, including positive index extraction and array manipulation, readers gain valuable insights into harnessing substring functions for precise string handling. By combining real-time examples with detailed explanations, this article equips users with the knowledge and creativity to enhance their automation flows and manage string data efficiently within the Power Automate platform.

Following are notable examples:

  • How to Extract a Substring from a Specific Index
  • How to Extract a Substring with Maximum Length
  • How to extract a substring from a SharePoint List Item
  • How to Extract a Substring from a CSV String
  • How to extract a substring from a JSON object in Power Automate
  • How to get a substring from an array in Power Automate
  • How to extract a substring from a URL in Power Automate

See Also: Power Platform Articles

You may also visit the Power Platform article hub, where you will see a bunch of articles focusing on Power Platform, like Power Automate, Power Apps, etc. All the articles are written with real-time project scenarios and troubleshooting techniques.

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment