When it comes to String manipulation, Dataweave 2.0 offers a myriad of great out-of-the-box functions.

However, even seasoned developers can begin to feel the frustration build when their Dataweave scripts don’t work as advertised; sometimes the Mule just appears to be stubborn.

One such area is in the String manipulation space. While writing your Dataweave scripts you may have found that even something as simple as a substring() function will not work in your Mule Runtime.

The function is well documented on the vendor’s website. You can even test your script in the vendor’s Dataweave Playground, where it works perfectly, as expected. And yet…

You find that the following script simply will not work on your Mule Runtime!

import * from dw::core::Strings
output application/java
---
substring(payload as String,0,3)

If you trawl the help forums long enough, you will probably discover that you are using an older Runtime version. This is where experienced developers are likely to become exasperated. Surely something as basic as substring() would be available even in much older versions!

That assumption is incorrect, as it turns out. The substring() function only works on v 4.4 and above.

While surprising, this hardly posed an insurmountable challenge.

Whisper!

A simple change of syntax will deliver the result you want.

output application/java
---
payload[0 to 2]

That is how to get a substring using older versions. Maybe not the most intuitive syntax but it does the job. Note the difference in the end number (changed from 3 to 2). This is not a typo. This is a necessary change to get the identical result to that in the substring() function, if you were using Mule Runtime 4.4+.

The lesson? Just because Mule doesn’t offer a function called substring() does not mean it doesn’t offer the capability. You just need to know how to Whisper.

In the hands of a Whisperer, the Mule will co-operate — and it always delivers.

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>