Hello friends, In this article we will see some common dataweave String functions from String core package(dw::core::Strings) along with their use-cases.
To use these functions in dataweave scripts we need to add/import the module header as below.
you can either import complete package by using * or if you want to use specific function you can use just function name in place of placeholder as given below <functionName>
%dw 2.0
import * from dw::core::Strings or import <functionName> from dw::core::Strings
output application/json
---
{}
Here, we will see 4 common String functions and their usage.
- capitalize(String): String
- camelize(String): String
- dasherize(String): String
- underscore(String): String
capitalize(String): String
%dw 2.0
import capitalize from dw::core::Strings
output application/json
---
{
"a" : capitalize("manish"),
"b" : capitalize("mr_manish_kumar"),
"c" : capitalize("manish-KUMAR"),
"d" : capitalize("manishKumar")
}
Output:
{
"a": "Manish",
"b": "Mr Manish Kumar",
"c": "Manish Kumar",
"d": "Manish Kumar"
}
camelize(String): String
This function accepts input as String and returns results as String. It returns a string in camel case based on underscores in the string. All underscores are deleted, including any underscores at the beginning of the string.
%dw 2.0
import camelize from dw::core::Strings
output application/json
---
{
"a" : camelize("1_manish"),
"b" : camelize("mr_manish_kumar"),
"c" : camelize("_manish_kumar")
}
Output:
{
"a": "1Manish",
"b": "mrManishKumar",
"c": "manishKumar"
}
dasherize(String): String
This function accepts input as String and returns results as String. It replaces spaces, underscores, and camel-casing in a string with dashes (hyphens). If no spaces, underscores, and camel-casing are present, the output will match the input.
%dw 2.0
import dasherize from dw::core::Strings
output application/json
---
{
"a" : dasherize("Manish"),
"b" : dasherize("Manish_first_name"),
"c" : dasherize("Manish Kumar"),
"d" : dasherize("ManishKumar")
}
Output:
{
"a": "manish",
"b": "manish-first-name",
"c": "manish-kumar",
"d": "manish-kumar"
}
Above function works based String input containing underscore. Below underscore function will help you to revert String to underscore format.
underscore(String): String
This function accepts input as String and returns results as String. It replaces hyphens, spaces, and camel-casing in a string with underscores. If no hyphens, spaces, and camel-casing are present, the output will match the input. Also it converts string into lower case.
%dw 2.0
import underscore from dw::core::Strings
output json
---
{
"a" : underscore("Manish"),
"b" : underscore("Manish-first-name"),
"c" : underscore("Manish KUMAR"),
"d" : underscore("ManishKumar")
}
Output:
{
"a": "manish",
"b": "manish_first_name",
"c": "manish_kumar",
"d": "manish_kumar"
}
Also, If you want to use multiple functions in your transformation
you can import with either * or comma separated as example below:
%dw 2.0
import dasherize,underscore,capitalize,camelize from dw::core::Strings
output json
---
{
"a" : capitalize("manish_kumar"),
"b" : underscore("Mr-Manish-Kumar"),
"c" : dasherize("Manish KUMAR"),
"d" : camelize("manish_kumar")
}
Output:
{
"a": "Manish Kumar",
"b": "mr_manish_kumar",
"c": "manish-kumar",
"d": "manishKumar"
}
Note:: dasherize, underscore will convert result strings into lower case.
Happy Learning :)