hi i could not find a function in Dataweave to split a string by a specified length .
The string i need to split is
ThisistheStringineedtoSplit
Expected out put is an array with all the substrings
payload.splitBy(sizeOf(payload)/10)
[ThisistheString,ineedtoSplit]
We can use a recursive function along with the built in functionality around string indexes. With strings, you can select a substring easily like [0 to end]
. To then get the rest of the string you can do [end to -1]
. Using a negative index translates to an index from the back, so -1 will be the last character. Since this is a zero index, we grab [0 to size - 1]
to get the right number of characters, and then just recursively call again on [size to -1]
.
%dw 2.0
output application/json
fun divideBy(str: String, size: Number): Array<String> =
if (sizeOf(str) <= size) [str]
else [str[0 to size - 1]] ++ divideBy(str[size to -1], size)
var message = "ThisistheStringineedtoSplit"
---
message divideBy 10
Output:
[
"ThisistheS",
"tringineed",
"toSplit"
]
In your example output, the substrings are longer than 10 characters. Was that a mistake, or am I misunderstanding what you want?
Edit:
You could also do this with some regex, though I don't think there is any real gain here.. I just happened to have regex on the mind from a conversation yesterday.
%dw 2.0
output application/json
fun divideBy(str: String, size: Number): Array<String> =
flatten(str scan ".{1,$(size)}")
var message = "ThisistheStringineedtoSplit"
---
message divideBy 10