I introduced you to DataWeave to transform data from this format to different formats in the Mule application. To do this, in addition to understanding the syntax to use in DataWeave, we need to know more operations that DataWeave supports to transform our data more easily. To begin, in this tutorial, I will list the basic operations to manipulate the String that DataWeave 1.0 supports!
Table of Contents
Split string
To split a string in DataWeave, we need to use the square brackets, inside is the index of the character we will split.
For example, we have the payload of “Huong Dan Java” then
1 |
payload[0..2] |
will return the “Huo” value as follows:
Reverse the string
In order to reverse the string, we only need to declare the range of values from -1 to 0:
1 |
payload[-1..0] |
as follows:
The replace function
To replace any string in DataWeave, you can use replace keyword and with keyword, simple as follows:
1 |
payload replace "Java" with "Khanh" |
Result:
The sizeOf function
This sizeOf function supports multiple data types, depending on the data type of the parameter, the return value will be different. For String, it will return the length of the string. You declare it as follows:
1 |
sizeOf payload |
Result:
Concat strings
In DataWeave, to join any two or more strings, we will use the “++” character, for example:
1 |
payload ++ " Khanh Nguyen" |
Result:
The trim function
Like in Java, the trim function is used to remove spaces at the beginning and the end of any string. In DataWeave, we declare this method as follows:
1 |
trim payload |
Result:
The capitalize function
DataWeave allows us to uppercase the first character of any word using capitalize function as follows:
1 |
capitalize payload |
Result:
The camelize function
This function helps us to edit any string follow camelcase string as follows:
1 |
camelize payload |
Result:
The dasherize function
This function is used to add “-” between words in a string.
1 |
dasherize payload |
Result:
The pluralize method
This method is a bit peculiar because it only helps us correct a noun in English in plural form, for example, we have “mouse” in English in singular, plural will be “mice”. You use it as follows:
1 |
pluralize payload |
For example:
The singularize function
Similar to the pluralize function, but its purpose is the opposite:
1 |
singularize payload |
For example:
The lower function
Lowercase a string:
1 |
lower payload |
For example:
The upper function
This is the capitalization for a string:
1 |
upper payload |
For example: