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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • ITBench, Part 1: Next-Gen Benchmarking for IT Automation Evaluation
  • Apache Spark 4.0: Transforming Big Data Analytics to the Next Level
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • How to Format Articles for DZone
  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
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!