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

  • Proper Java Exception Handling
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • JSON-Based Serialized LOB Pattern
  • Adding Two Hours in DataWeave: Mule 4

Trending

  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  1. DZone
  2. Data Engineering
  3. Data
  4. DataWeave 2.4.0 Newly Added Functions in Strings Module

DataWeave 2.4.0 Newly Added Functions in Strings Module

In DataWeave version 2.4.0, MuleSoft has added some new functions in the String Module. Read to learn more about newly added functions and how we can use them.

By 
Anand Joshi user avatar
Anand Joshi
·
Updated Oct. 13, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
29.7K Views

Join the DZone community and get the full member experience.

Join For Free

DataWeave is a programming language used in MuleSoft for accessing and transforming data.   MuleSoft has released DataWeave 2.4.0 for Mule Runtime 4.4.

In DataWeave, functions are packaged inside a module and we can import the module in our DataWeave scripts to use the function. One such module is Strings (dw::core::Strings), which contains functions that are useful while working with Strings. In DataWeave version 2.4.0, MuleSoft has added some new functions in the String Module. In this blog, we will see some of those newly added functions and how we can use them.

1. collapse

This function is used to collapse or break the string into substrings of equal characters.

Note: empty space (" ") is treated as a character. More than one continuous occurrence of any character would be treated as a single substring.  For example, in word access, characters c and s have 2 occurrences.  

Example:

DataWeave Script:  

JSON
 
%dw 2.0
import collapse from dw::core::Strings
output application/json
---
{
"Output1"  : collapse("This is a test"),
"Output2"  : collapse("access")
}

Output: 

JSON
 
{
  "Output1": [
    "T",
    "h",
    "i",
    "s",
    " ",
    "i",
    "s",
    " ",
    "a",
    " ",
    "t",
    "e",
    "s",
    "t"
  ],
  "Output2": [
    "a",
    "cc",
    "e",
    "ss"
  ]
}

2. countCharactersBy

This function iterates through each character and counts the number of times when the value of the expression is true.

Example: 

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"StringLength" : sizeOf("Address is 123-ABC Street"),
"Alphabet Count"  : "Address is 123-ABC Street" countCharactersBy isAlpha($),
"Number Count"  : "Address is 123-ABC Street" countCharactersBy isNumeric($),
"AplhaNumberic Count" : "Address is 123-ABC Street" countCharactersBy isAlphanumeric($),
"Whitespace Count" : "Address is 123-ABC Street" countCharactersBy  isWhitespace($)
}

Output:

JSON
 
{
  "StringLength": 25,
  "Alphabet Count": 18,
  "Number Count": 3,
  "AplhaNumberic Count": 21,
  "Whitespace Count": 3
}

3. countMatches

This function counts the number of times a pattern appears in a string.

Example:  

Dataweave Script:

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "Dataweave is fun, Dataweave is interesting!" countMatches "is",
}

Output:

JSON
 
Output  : 
{
  "Output1": 2
}

4. everyCharacter

This function checks whether the given DataWeave expression is valid for every character in a string and returns Boolean value.

Example:

DataWeave Script:  

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 : "THIS IS UPPER CASE" everyCharacter $ == " " or isUpperCase($),
Output2 : "THIS IS NUMBER 123" everyCharacter $ == " " or isUpperCase($)
}

Output: 

JSON
 
{
  "Output1": true,
  "Output2": false
}

5. first 

This function returns the first n number of characters in the string. If the value of n is equal to or greater than the number of characters in the string, then the function returns the entire string.

Example: 

DataWeave Script:  

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "Avengers Assemble!" first 8,
"Output2" : "Avengers Assemble!" first 80
}

Output:

JSON
 
{
  "Output1": "Avengers",
  "Output2": "Avengers Assemble!"
}

6. last

This function returns the last n number of characters in the string. If the value of n is greater than or equal to the string length, then the whole string is returned.

Example:

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "I am Ironman" last 7,
"Output2" : "I am Ironman" last 70
}

Output: 

JSON
 
{
  "Output1": "Ironman",
  "Output2": "I am Ironman"
}

7. lines 

This function takes String as input and returns an array.

Note: \n in the string refers to a new line. So the occurrence of \n results in the next element of the array.

Example:

DataWeave Script:

JSON
 
%dw 2.0
import lines from dw::core::Strings
output application/json
---
{
output1 : lines("This is a sample String"),    
output2 : lines("This is an example\n which has many lines\n like this")
}

Output: 

Java
 
{
  "output1": [
    "This is a sample String"
  ],
  "output2": [
    "This is an example",
    " which has many lines",
    " like this"
  ]
}

8. mapString  

This function applies an expression to every character of a string.

Example: 

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1: ("_ is fun and _ is interesting") mapString if ($ == "_") "Dataweave" else $,
output2 : ("Password") mapString '*'
}

Output: 

JSON
 
{
  "output1": "Dataweave is fun and Dataweave is interesting",
  "output2": "********"
}

9. remove

This function removes all occurrences of a specified pattern from a string.

Example:

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1 : "This is a sample String" remove "s"
}

Output: 

JSON
 
{
  "output1": "Thi i a ample String"
}

10. replaceAll 

This function takes a string and replaces all the matching substrings with the specified replacement String.

Example: 

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 : replaceAll("I am Batman", "Batman" , "Ironman"), 
Output2 : replaceAll("aaa aaaaa" , "aa" , "A"),
Output3 : replaceAll("Dataweave is tricky", "tricky" , "fun"), 
}

Output: 

JSON
 
{
  "output1": "I am Ironman",
  "output2": "Aa AAa",
  "output3": "Dataweave is fun",
}

11. reverse

This function is used to reverse the sequence of characters in a string.

Example:

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 : reverse("Dataweave is fun")
}

Output: 

JSON
 
{
"Output1": "nuf si evaewataD"
}

12. someCharacter  

This function is used to check whether a condition is valid for at least one of the characters or blank spaces in a string.

Example:

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
outupt1 : "this string has 4 words" someCharacter isNumeric($),
output2 : "is there any character in upper case" someCharacter isUpperCase($)
}

Output: 

JSON
 
{
  "outupt1": true,
  "output2": false
}

13. substring

This function is used to get a substring from the string by passing the start index and end index. 

Note:  startIndex is inclusive and endIndex is exclusive. In the example below, 0 is the start index and 4 is the end index. As the end index is exclusive, this is why we are getting output from characters 0 to 3 (i.e. Data). 

Example:

DataWeave Script: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
outupt1 : substring("Dataweave is fun", 0, 4)
}

Output:

JSON
 
{
  "outupt1": "Data"
}

14. substringBy

This function is used to split a string at each character where the specified expression returns true. This function takes String as input and gives an array of substrings.

Example: 

DataWeave Script:

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 :  "This_is_Sample_Example" substringBy $ =="_",
Output2 :  "Fantastic Repetation"  substringBy $ =="t"
}

Output:

JSON
 
{
"Output1": [
    "This",
    "is",
    "Sample",
    "Example"
  ],
  "Output2": [
    "Fan",
    "as",
    "ic Repe",
    "a",
    "ion"
  ]
}

15. substringEvery

This function splits the given string into an array of equal substrings of a specified length.

Example:

DataWeave Script:

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1 : substringEvery("This is a test", 3)
}

Output:

JSON
 
{
  "output11": [
    "Thi",
    "s i",
    "s a",
    " te",
    "st"
  ]
}

16. words

This function takes a string as input and returns an array of words. It separates the words in a string using separators like blank space, new lines (\n), and tabs (\t).  

Example: 

DataWeave Expression: 

JSON
 
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1 : words("Dataweave is interesting"),
output2 : words("Dataweave\nis\nfun"),
output3 : words("Do\tyou\tlike\tDataweave?")
}

Output:

JSON
 
{
  "output1": [
    "Dataweave",
    "is",
    "interesting"
  ],
  "output2": [
    "Dataweave",
    "is",
    "fun"
  ],
  "output3": [
    "Do",
    "you",
    "like",
    "Dataweave?"
  ]
}

Now we know about these newly added DataWeave string functions. For more details about DataWeave string functions, please check the Mulesoft documentation here. 

Strings JSON Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Proper Java Exception Handling
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • JSON-Based Serialized LOB Pattern
  • Adding Two Hours in DataWeave: Mule 4

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