Wednesday, May 17, 2017

String manipulation with Ballerina

Ballerina is one of the coolest things going on at work at the moment, a passion project initiated by WSO2's founder Sanjiva Weerawarana, it was birthed from the experiences amassed through over a decade of creating industry leading Middleware products and helping organizations succeed in their SOA based projects.

Whether you are integrating enterprise systems over different transports, messaging conventions, adding mediation logic to existing services or writing some code for your pet IoT project you will be dealing with a lot of string manipulations. Now, string manipulation is not something a programmer needs to learn repeatedly, but since Ballerina is a whole new beast of a programming language(in the making) it might be advisable to invest a bit of time to get familiar with the its string manipulation capabilities.

Ballerina provides a bunch of string utility methods somewhat similar to python, packed under ballerina.lang.strings. Look to the Strings API documentation[1] for a complete list of methods. Apart from these utility methods, Ballerina supports escaping(“\””) and concatenation(+) same as Java and provide inline formatting capabilities such as “\t” for tab and “\n” for newline.

The code segment found below shows how some of the methods discussed above can be used in a real world scenario where a header value passed into a ballerina service and depending on  the callers time a response is generated and sent out.

Try out the ballerina script above with these invocations,

[1] -

Note: this example was written for ballerina 0.86 and it may not work with other versions.

No comments:

Post a Comment