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

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago