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

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

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

  • KubeVirt: Can VM Management With Kubernetes Work?
  • OTel Me Why: The Case for OpenTelemetry Beyond the Shine
  • Agile’s Quarter-Century Crisis
  • Agile and Quality Engineering: A Holistic Perspective
  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.0K 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.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: