DZone
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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • When One MVP Is Really Four Systems: A Better Way to Plan Multi-Role Apps
  • The Big Data Architecture Blueprint: Core Storage, Integration, and Governance Patterns
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  1. DZone
  2. Data Engineering
  3. Data
  4. 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.

By 
Rakesh Kumar Jha user avatar
Rakesh Kumar Jha
·
Oct. 12, 17 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
68.5K 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.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook