DZone
Integration Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > String Manipulation - Substrings in Mule

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.

Rakesh Kumar Jha user avatar by
Rakesh Kumar Jha
·
Oct. 12, 17 · Integration Zone · Tutorial
Like (7)
Save
Tweet
61.62K Views

Join the DZone community and get the full member experience.

Join For Free

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">
      <expression-language>
          <global-functions>
              def subString(value,start,end) {
                  return org.apache.commons.lang3.StringUtils.substring(value,start,end);
              }
          </global-functions>
      </expression-language>
  </configuration>

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!

Strings Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is ERP Testing? - A Brief Guide
  • 3 Predictions About How Technology Businesses Will Change In 10 Years
  • Unit Vs Integration Testing: What's the Difference?
  • How To Integrate Third-Party Login Systems in Your Web App Using OAuth 2.0

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo