This blog post will guide you through using DWL’s lines function. This function is a useful tool for handling strings with line breaks. The lines function takes a string as input. It outputs an array of strings. Each one represents a line from the source string. The newline () or carriage return () characters are used by the function to divide the string.
Example 1: Dividing a string according to line breaks
Suppose we have a string with some text in it that has line breaks, something like this:
%dw 2.0import * from dw::core::Stringsvar myString = "Hello,\nthis is a\nsample string."output application/json ---/* If we want to split this string by line breaks and store each line in an array, we can use the lines function like this: */lines(myString)An array of three strings is the outcome:
[ "Hello,", "this is a", "sample string."]Using the index notation, such as myArray[0] or myArray[2], we may access each element of the array.
Joining an array of strings by line breaks is example number two.
In contrast to the lines method, the joinBy function returns a single string. It accepts an array of strings as inputs and a separator. Any string, including a space, comma, or line break, can be used as the separator. To join the array from the preceding example by line breaks, for instance, we may use the joinBy method as follows:
var myString = joinBy(myArray, "\n")The initial string is the outcome:
"Hello,\nthis is a\nsample string."Example 3: Handling a string line by line
Processing a string’s individual lines is a typical use case for the lines function. Let’s take an example where we have a string with some CSV-formatted data in it, such as this:
var myString = "name,age,gender\nAlice,25,F\nBob,30,M\nCharlie,28,M"These functions may be used to split the string by line breaks and then map each line into an object. We can turn this string into an array of objects. Each object will have the attributes name, age, and gender. The lambda expression and array are passed to the map function. It produces a new array with each element derived from the lambda expression. The expression is applied to its corresponding element in the original array. To reference the current element, the lambda expression can use the $ symbol. To transform a string into an array of objects, for instance, we can utilize the code below:
%dw 2.0import * from dw::core::Stringsvar myArray = lines(payload)output application/json ---myArray[1 to -1] map ({ ((myArray[0] splitBy ",") map ((item, index) -> (item): ($ splitBy ",")[index] ))})The string can also be transformed into an array of objects using a variety of techniques.
I’d want to show you how many different methods we can turn the string into an array of items.
%dw 2.0output application/json ---read(payload, "application/json")An array of three objects is the end result:
[ { "name": "Alice", "age": 25, "gender": "F" }, { "name": "Bob", "age": 30, "gender": "M" }, { "name": "Charlie", "age": 28, "gender": "M" }]The index notation, like myArray[1], allows us to access any object or any of its properties.either myArray[2] or name.sexual orientation.
In conclusion, working with strings that contain line breaks is made easier with the help of DWL’s lines function. It can parse a string line by line. It can also split a string by line breaks. Additionally, it can combine an array of strings by line breaks. I hope you gained some fresh insight about DWL from this blog post. Please feel free to remark below if you have any queries or comments.
]]>