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


This function accepts input as String and returns results as String. It capitalizes the first letter of each word in a string. It also removes underscores between words and puts a space before each capitalized word.

%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 🙂

By Manish Kumar

I am having around 10 years of IT experience in Integration Architecture, Requirement gathering, Effort Estimation, Application Design\Development\Testing and Deployment including 5+ years of experience in MuleSoft ESB and Hybrid Integrations. DevOps and Cloud Integration is my area of interest.

6 thoughts on “Dataweave useful String functions-Part1”
  1. Youre so cool! I dont suppose Ive read something like this before. So nice to search out any person with some unique ideas on this subject. realy thank you for starting this up. this website is one thing that is needed on the net, someone with somewhat originality. useful job for bringing one thing new to the internet!

  2. It’s a pity you don’t have a donate button! I’d certainly donate to this brilliant blog! I suppose for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this blog with my Facebook group. Chat soon!

  3. Hey I know this is off topic but I was wondering
    if you knew of any widgets I could add to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your new updates.

  4. เครดิตฟรี 50 ไม่ต้องฝากไม่ต้องแชร์ says:

    It’s fantastic that you are getting thoughts from this piece of writing as well as from our dialogue made at this
    time.

Leave a Reply

Your email address will not be published. Required fields are marked *