splitBy function in DataWeave 2.0 (mulesoft)

Overviews

Splits a string into a string array based on a value of input or matches part of that input string. It filters out the matching part from the returned array. A string or a Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array.

This version of splitBy accepts a Java regular expression (regex) to match the input string. The regex can match any character in the input string. Note that splitBy performs the opposite operation of joinBy.

splitBy(text: String, regex: Regex): Array<String>

Parameters

NameDescription
textThe input string to split.
regexA Java regular expression used to split the string. If it does not match some part of the string, the function will return the original, unsplit string in the array.

Example

This example uses a Java regular expression to split an address block by the periods and forward slash in it. Notice that the regular expression goes between forward slashes.

Input
 "Hello world"
Dataweave 2.0 Expression
%dw 2.0
output application/java
---
payload splitBy(/s/)
Output
[Hello world]

Source
%dw 2.0
output application/json
---
"192.88.99.0/24" splitBy(/[.\/]/)
Output
["192", "88", "99", "0", "24"]

Splits a string into a string array based on a separating string that matches part of the input string. It also filters out the matching string from the returned array.

The separator can match any character in the input. Note that splitBy performs the opposite operation of joinBy.

splitBy(text: String, separator: String): Array<String>

Parameters

NameDescription
textThe string to split.
separatorA string used to separate the input string. If it does not match some part of the string, the function will return the original, unsplit string in the array.

Example

This example splits a string containing an IP address by its periods.

Source
%dw 2.0
output application/json
---
"192.88.99.0" splitBy(".")
Output
["192", "88", "99", "0"]

splitBy(text: Null, separator: Any)

Helper function that enables splitBy to work with a null value.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *