String Manipulation - Substrings in Mule

DZone 's Guide to

String Manipulation - Substrings in Mule

In this tutorial, you will learn how to perform substring operations on string data in Mule flows, useful in API implementations, for example.

· Integration Zone ·
Free Resource

As a MuleSoft Certified Architect, Designer and Developer, I recently worked on API implementations for one of our clients using MuleSoft’s CloudHub. One common feature that we needed to use across APIs implementations is to perform substring operations in Mule flows to extracts expected characters from string data.

In this blog, I will demonstrate different ways to perform substring operation on string data in the Mule Flow.

Using StringUtils to Manipulate Strings in Mule Flows

The StringUtils class of Java contains useful methods for manipulating Strings. We will use this class in our Mule flow to perform substring operation.

We first have to define a global function using the org.apache.commons.lang3.StringUtils class, and then we will use this global function from our Mule components to perform substring operations.

The code below will create a global function named “subString,” inheriting substring functions of the StringUtils class.  

<configuration doc:name="Configuration">
              def subString(value,start,end) {
                  return org.apache.commons.lang3.StringUtils.substring(value,start,end);

Now we can use this global function in our Mule flow to perform a substring operation. The code snippet below shows subString function has been used in a variable component of the Mule flow to perform a substring operation on payload data.

<set-variable variableName="Name" value="#[subString(payload,0,6)]" doc:name="Variable"/>

We can also access this function from Mule Dataweave. As shown in the below screenshot, this function has been used to perform a substring operation to extract the first name and last name from a payload element name from Mule Dataweave.

Image title

This approach is very useful when we have to perform substring operations from Mule Dataweave or to perform substring operations from multiple activities in the Mule flow. In other cases, we can directly use this StringUtils class in our Mule components, as shown below:

<set-variable variableName="Name" value="#[org.apache.commons.lang3.StringUtils.substring(payload,0,6)]" doc:name="Variable"/>

Using the Range Selector Operator

Mule provides selector expressions to navigate in strings, objects, or array elements in Mule Dataweave. We will use the Range Selector operator to extract characters by specifying a range.

As the below screen demonstrates, we have used the Mule Range Selector operator in Dataweave to extract the first name and last name from the payload element name.

Image title

Let’s share our knowledge to expand our MuleSoft community. Thank you!

mulesoft ,mule studio ,dataweave ,substring ,java ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}