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.
Join the DZone community and get the full member experience.
Join For FreeDataWeave 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:
%dw 2.0
import collapse from dw::core::Strings
output application/json
---
{
"Output1" : collapse("This is a test"),
"Output2" : collapse("access")
}
Output:
{
"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:
%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:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "Dataweave is fun, Dataweave is interesting!" countMatches "is",
}
Output:
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:
%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:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "Avengers Assemble!" first 8,
"Output2" : "Avengers Assemble!" first 80
}
Output:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"Output1" : "I am Ironman" last 7,
"Output2" : "I am Ironman" last 70
}
Output:
{
"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:
%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:
{
"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:
%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:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1 : "This is a sample String" remove "s"
}
Output:
{
"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:
%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:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 : reverse("Dataweave is fun")
}
Output:
{
"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:
%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:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
outupt1 : substring("Dataweave is fun", 0, 4)
}
Output:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
Output1 : "This_is_Sample_Example" substringBy $ =="_",
Output2 : "Fantastic Repetation" substringBy $ =="t"
}
Output:
{
"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:
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
output1 : substringEvery("This is a test", 3)
}
Output:
{
"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:
%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:
{
"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.
Opinions expressed by DZone contributors are their own.
Comments