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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago