This module contains helper functions to work with Strings.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Strings
to the header of your
DataWeave script.
Functions
appendIfMissing
appendIfMissing(String, String): String
Appends the suffix
to the end of the text
if the text
does not already
ends with the suffix
.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The text used as the suffix. |
Example
This example shows how appendIfMissing
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import appendIfMissing from dw::core::Strings
output application/json
---
{
"a": appendIfMissing(null, ""),
"b": appendIfMissing("abc", ""),
"c": appendIfMissing("", "xyz") ,
"d": appendIfMissing("abc", "xyz") ,
"e": appendIfMissing("abcxyz", "xyz")
}
Output
1
2
3
4
5
6
7
{
"a": null,
"b": "abc",
"c": "xyz",
"d": "abcxyz",
"e": "abcxyz"
}
appendIfMissing(Null, String): Null
Helper function to make appendIfMissing
work with null
value.
camelize
camelize(String): String
Returns a string in camel case based on underscores in the string. If it starts with underscore they are being deleted
Parameters
Name | Description |
---|---|
|
The string to convert to camel case. |
Example
This example converts a string that contains underscores to camel case.
Source
1
2
3
4
5
6
7
8
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : camelize("customer_first_name"),
"b" : camelize("_name_starts_with_underscore")
}
Output
1
2
3
4
{
"a": "customerFirstName",
"b": "nameStartsWithUnderscore"
}
camelize(Null): Null
Helper function that allows camelize
to work with null values.
capitalize
capitalize(String): String
Capitalizes the first letter of each word in a string.
It also removes underscores between words and puts a space before each capitalized word.
Parameters
Name | Description |
---|---|
|
The string to capitalize. |
Example
This example capitalizes a set of strings.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : capitalize("customer"),
"b" : capitalize("customer_first_name"),
"c" : capitalize("customer NAME"),
"d" : capitalize("customerName")
}
Output
1
2
3
4
5
6
{
"a": "Customer",
"b": "Customer First Name",
"c": "Customer Name",
"d": "Customer Name"
}
capitalize(Null): Null
Helper function that allows capitalize to work with null values.
charCode
charCode(String): Number
Returns the Unicode for the first character in an input string.
For an empty string, the function fails and returns Unexpected empty string
.
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example returns Unicode for the "M" in "Mule".
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"charCode" : charCode("Mule")
}
Output
1
{ "charCode" : 77 }
charCodeAt
charCodeAt(String, Number): Number
Returns the Unicode for a character at the specified index.
This function fails if the index is invalid.
Parameters
Name | Description |
---|---|
|
The input string. |
|
The index (a |
Example
This example returns Unicode for the "u" at index 1
in "MuleSoft".
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"charCodeAt" : charCodeAt("MuleSoft", 1)
}
Output
1
{ "charCodeAt": 117 }
dasherize
dasherize(String): String
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.
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example replaces the spaces, underscores, and camel-casing in the input. Notice that the input "customer" is not modified in the output.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : dasherize("customer"),
"b" : dasherize("customer_first_name"),
"c" : dasherize("customer NAME"),
"d" : dasherize("customerName")
}
Output
1
2
3
4
5
6
{
"a": "customer",
"b": "customer-first-name",
"c": "customer-name",
"d": "customer-name"
}
dasherize(Null): Null
Helper function that allows dasherize to work with null values.
fromCharCode
fromCharCode(Number): String
Returns a character that matches the specified Unicode.
Parameters
Name | Description |
---|---|
|
The input Unicode (a |
Example
This example inputs the Unicode number 117
to return the character "u".
Source
1
2
3
4
5
6
7
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"fromCharCode" : fromCharCode(117)
}
Output
1
{ "fromCharCode": "u" }
isAlpha
isAlpha(String): Boolean
Checks if the text
contains only Unicode digits. A decimal point is not a Unicode digit and returns false
.
Note that the method does not allow for a leading sign, either positive or negative.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isNumeric
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import isAlpha from dw::core::Strings
output application/json
---
{
"a": isAlpha(null),
"b": isAlpha(""),
"c": isAlpha(" "),
"d": isAlpha("abc"),
"e": isAlpha("ab2c"),
"f": isAlpha("ab-c")
}
Output
1
2
3
4
5
6
7
8
{
"a": false,
"b": false,
"c": false,
"d": true,
"e": false,
"f": false
}
isAlpha(Null): Boolean
Helper function for isAlpha
so it works with null
value.
isAlphanumeric
isAlphanumeric(String): Boolean
Checks if the text
contains only Unicode letters or digits.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isAlphanumeric
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
import isAlphanumeric from dw::core::Strings
output application/json
---
{
"a": isAlphanumeric(null),
"b": isAlphanumeric(""),
"c": isAlphanumeric(" "),
"d": isAlphanumeric("abc"),
"e": isAlphanumeric("ab c"),
"f": isAlphanumeric("ab2c"),
"g": isAlphanumeric("ab-c")
}
Output
1
2
3
4
5
6
7
8
9
{
"a": false,
"b": false,
"c": false,
"d": true,
"e": false,
"f": true,
"g": false
}
isAlphanumeric(Null): Boolean
Helper function for isAlphanumeric
so it works with null
value.
isLowerCase
isLowerCase(String): Boolean
Checks if the text
contains only lowercase characters.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isNumeric
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%dw 2.0
import isLowerCase from dw::core::Strings
output application/json
---
{
"a": isLowerCase(null),
"b": isLowerCase(""),
"c": isLowerCase(" "),
"d": isLowerCase("abc"),
"e": isLowerCase("aBC"),
"f": isLowerCase("a c"),
"g": isLowerCase("a1c"),
"h": isLowerCase("a/c")
}
Output
1
2
3
4
5
6
7
8
9
10
{
"a": false,
"b": false,
"c": false,
"d": true,
"e": false,
"f": false,
"g": false,
"h": false
}
isLowerCase(Null): Boolean
Helper function for isLowerCase
so it works with null
value.
isNumeric
isNumeric(String): Boolean
Checks if the text
contains only Unicode digits.
A decimal point is not a Unicode digit and returns false. Note that the method does not allow for a leading sign, either positive or negative.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isNumeric
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%dw 2.0
import isNumeric from dw::core::Strings
output application/json
---
{
"a": isNumeric(null),
"b": isNumeric(""),
"c": isNumeric(" "),
"d": isNumeric("123"),
"e": isNumeric("१२३"),
"f": isNumeric("12 3"),
"g": isNumeric("ab2c"),
"h": isNumeric("12-3"),
"i": isNumeric("12.3"),
"j": isNumeric("-123"),
"k": isNumeric("+123")
}
Output
1
2
3
4
5
6
7
8
9
10
11
12
13
{
"a": false,
"b": false,
"c": false,
"d": true,
"e": true,
"f": false,
"g": false,
"h": false,
"i": false,
"j": false,
"k": false
}
isNumeric(Null): Boolean
Helper function for isNumeric
so it works with null
value.
isUpperCase
isUpperCase(String): Boolean
Checks if the text
contains only uppercase characters.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isNumeric
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%dw 2.0
import isUpperCase from dw::core::Strings
output application/json
---
{
"a": isUpperCase(null),
"b": isUpperCase(""),
"c": isUpperCase(" "),
"d": isUpperCase("ABC"),
"e": isUpperCase("aBC"),
"f": isUpperCase("A C"),
"g": isUpperCase("A1C"),
"h": isUpperCase("A/C")
}
Output
1
2
3
4
5
6
7
8
9
10
{
"a": false,
"b": false,
"c": false,
"d": true,
"e": false,
"f": false,
"g": false,
"h": false
}
isUpperCase(Null): Boolean
Helper function for isLowerCase
so that it works with null
value.
isWhitespace
isWhitespace(String): Boolean
Checks if the text
contains only whitespace.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example shows how isWhitespace
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import isWhitespace from dw::core::Strings
output application/json
---
{
"a": isWhitespace(null),
"b": isWhitespace(""),
"c": isWhitespace(" "),
"d": isWhitespace("abc"),
"e": isWhitespace("ab2c"),
"f": isWhitespace("ab-c")
}
Output
1
2
3
4
5
6
7
8
{
"a": false,
"b": true,
"c": true,
"d": false,
"e": false,
"f": false
}
isWhitespace(Null): Boolean
Helper function for isWhitespace
so it works with null
value.
leftPad
leftPad(String, Number, String): String
The specified text
is left-padded to the size
using the padText
.
By default padText
is " "
.
Returns left-padded String
or original String
if no padding is necessary.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The size to pad to. |
|
The text to pad with. It defaults to one space if not specified. |
Example
This example shows how leftPad
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": leftPad(null, 3),
"b": leftPad("", 3),
"c": leftPad("bat", 5),
"d": leftPad("bat", 3),
"e": leftPad("bat", -1)
}
Output
1
2
3
4
5
6
7
{
"a": null,
"b": " ",
"c": " bat",
"d": "bat",
"e": "bat"
}
leftPad(Null, Number, String): Null
Helper function to make leftPad
work with null
value.
ordinalize
ordinalize(Number): String
Returns a number as an ordinal, such as 1st
or 2nd
.
Parameters
Name | Description |
---|---|
|
An input number to return as an ordinal. |
Example
This example returns a variety of input numbers as ordinals.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : ordinalize(1),
"b": ordinalize(2),
"c": ordinalize(5),
"d": ordinalize(103)
}
Output
1
2
3
4
5
6
{
"a": "1st",
"b": "2nd",
"c": "5th",
"d": "103rd"
}
ordinalize(Null): Null
Helper function that allows ordinalize
to work with null values.
pluralize
pluralize(String): String
Pluralizes a singular string.
If the input is already plural (for example, "boxes"), the output will match the input.
Parameters
Name | Description |
---|---|
|
The string to pluralize. |
Example
This example pluralizes the input string "box" to return "boxes".
Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "pluralize" : pluralize("box") }
Output
1
{ "pluralize" : "boxes" }
pluralize(Null): Null
Helper function that allows pluralize to work with null values.
prependIfMissing
prependIfMissing(String, String): String
Prepends the prefix
to the beginning of the string if the text
does not
already start with that prefix.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The text to use as prefix. |
Example
This example shows how prependIfMissing
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import prependIfMissing from dw::core::Strings
output application/json
---
{
"a": prependIfMissing(null, ""),
"b": prependIfMissing("abc", ""),
"c": prependIfMissing("", "xyz"),
"d": prependIfMissing("abc", "xyz"),
"e": prependIfMissing("xyzabc", "xyz")
}
Output
1
2
3
4
5
6
7
{
"a": null,
"b": "abc",
"c": "xyz",
"d": "xyzabc",
"e": "xyzabc"
}
prependIfMissing(Null, String): Null
Helper function to make prependIfMissing
work with null
value.
repeat
repeat(String, Number): String
Repeats a text
the number of specified times
.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
Number of times to repeat char. Negative is treated as zero. |
Example
This example shows how repeat
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": repeat("e", 0),
"b": repeat("e", 3),
"c": repeat("e", -2)
}
Output
1
2
3
4
5
{
"a": "",
"b": "eee",
"c": ""
}
rightPad
rightPad(String, Number, String): String
The specified text
is right-padded to the size
using the padText
.
By default padText
is " "
.
Returns right padded String
or original String
if no padding is necessary.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The size to pad to. |
|
The text to pad with. It defaults to one space if not specified. |
Example
This example shows how rightPad
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": rightPad(null, 3),
"b": rightPad("", 3),
"c": rightPad("bat", 5),
"d": rightPad("bat", 3),
"e": rightPad("bat", -1)
}
Output
1
2
3
4
5
6
7
{
"a": null,
"b": " ",
"c": "bat ",
"d": "bat",
"e": "bat"
}
rightPad(Null, Number, String): Null
Helper function to make rightPad
work with null
value.
singularize
singularize(String): String
Converts a plural string to its singular form.
If the input is already singular (for example, "box"), the output will match the input.
Parameters
Name | Description |
---|---|
|
The string to convert to singular form. |
Example
This example converts the input string "boxes" to return "box".
Source
1
2
3
4
5
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "singularize" : singularize("boxes") }
Output
1
{ "singularize" : "box" }
singularize(Null): Null
Helper function that allows singularize to work with null values.
substringAfter
substringAfter(String, String): String
Gets the substring after the first occurrence of a separator. The separator is not returned.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
String to search for. |
Example
This example shows how substringAfter
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": substringAfter(null, "'"),
"b": substringAfter("", "-"),
"c": substringAfter("abc", "b"),
"d": substringAfter("abcba", "b"),
"e": substringAfter("abc", "d"),
"f": substringAfter("abc", "")
}
Output
1
2
3
4
5
6
7
8
9
{
"a": null,
"b": "",
"c": "c",
"d": "cba",
"e": "",
"f": "bc"
}
substringAfter(Null, String): Null
Helper function for substringAfter
work with null value.
substringAfterLast
substringAfterLast(String, String): String
Gets the substring after the last occurrence of a separator. The separator is not returned.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
String to search for. |
Example
This example shows how substringAfterLast
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": substringAfterLast(null, "'"),
"b": substringAfterLast("", "-"),
"c": substringAfterLast("abc", "b"),
"d": substringAfterLast("abcba", "b"),
"e": substringAfterLast("abc", "d"),
"f": substringAfterLast("abc", "")
}
Output
1
2
3
4
5
6
7
8
{
"a": null,
"b": "",
"c": "c",
"d": "a",
"e": "",
"f": null
}
substringAfterLast(Null, String): Null
Helper function for substringAfterLast
to work with null
value.
substringBefore
substringBefore(String, String): String
Gets the substring before the first occurrence of a separator. The separator is not returned.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
String to search for. |
Example
This example shows how substringBefore
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": substringBefore(null, "'"),
"b": substringBefore("", "-"),
"c": substringBefore("abc", "b"),
"d": substringBefore("abc", "c"),
"e": substringBefore("abc", "d"),
"f": substringBefore("abc", "")
}
Output
1
2
3
4
5
6
7
8
{
"a": null,
"b": "",
"c": "a",
"d": "ab",
"e": "",
"f": ""
}
substringBefore(Null, String): Null
Helper function for substringBefore
to work with null value.
substringBeforeLast
substringBeforeLast(String, String): String
Gets the substring before the last occurrence of a separator. The separator is not returned.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
String to search for. |
Example
This example shows how substringBeforeLast
behaves with different inputs
and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": substringBeforeLast(null, "'"),
"b": substringBeforeLast("", "-"),
"c": substringBeforeLast("abc", "b"),
"d": substringBeforeLast("abcba", "b"),
"e": substringBeforeLast("abc", "d"),
"f": substringBeforeLast("abc", "")
}
Output
1
2
3
4
5
6
7
8
{
"a": null,
"b": "",
"c": "ab",
"d": "abc",
"e": "",
"f": "a"
}
substringBeforeLast(Null, String): Null
Helper function for substringBeforeLast
to work with null value.
underscore
underscore(String): String
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.
Parameters
Name | Description |
---|---|
|
The input string. |
Example
This example replaces the hyphens and spaces in the input. Notice that the input "customer" is not modified in the output.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a" : underscore("customer"),
"b" : underscore("customer-first-name"),
"c" : underscore("customer NAME"),
"d" : underscore("customerName")
}
Output
1
2
3
4
5
6
{
"a": "customer",
"b": "customer_first_name",
"c": "customer_name",
"d": "customer_name"
}
underscore(Null): Null
Helper function that allows underscore to work with null values.
unwrap
unwrap(String, String): String
Unwraps a given text
from a wrapper
text.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The text used to unwrap. |
Example
This example shows how unwrap
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
import unwrap from dw::core::Strings
output application/json
---
{
"a": unwrap(null, ""),
"b": unwrap(null, '\0'),
"c": unwrap("'abc'", "'"),
"d": unwrap("AABabcBAA", 'A'),
"e": unwrap("A", '#'),
"f": unwrap("#A", '#'),
"g": unwrap("A#", '#')
}
Output
1
2
3
4
5
6
7
8
9
{
"a": null,
"b": null,
"c": "abc",
"d": "ABabcBA",
"e": "A",
"f": "A#",
"g": "#A"
}
unwrap(Null, String): Null
Helper function to make unwrap
work with null
value.
wrapIfMissing
wrapIfMissing(String, String): String
Wraps text
with wrapper
if that wrapper
is missing from the start or
end of the given string.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The content used to wrap. |
Example
This example shows how wrapIfMissing
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": wrapIfMissing(null, "'"),
"b": wrapIfMissing("", "'"),
"c": wrapIfMissing("ab", "x"),
"d": wrapIfMissing("'ab'", "'"),
"e": wrapIfMissing("/", '/'),
"f": wrapIfMissing("a/b/c", '/'),
"g": wrapIfMissing("/a/b/c", '/'),
"h": wrapIfMissing("a/b/c/", '/')
}
Output
1
2
3
4
5
6
7
8
9
10
{
"a": null,
"b": "'",
"c": "xabx",
"d": "'ab'",
"e": "/",
"f": "/a/b/c/",
"g": "/a/b/c/",
"h": "/a/b/c/"
}
wrapIfMissing(Null, String): Null
Helper function for wrapIfMissing
to work with null
value.
wrapWith
wrapWith(String, String): String
Wraps the specified text
with the given wrapper
.
Since Version: 2.2.0
Parameters
Name | Description |
---|---|
|
The input string. |
|
The content used to wrap. |
Example
This example shows how wrapWith
behaves with different inputs and sizes.
Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import * from dw::core::Strings
output application/json
---
{
"a": wrapWith(null, "'"),
"b": wrapWith("", "'"),
"c": wrapWith("ab", "x"),
"d": wrapWith("'ab'", "'"),
"e": wrapWith("ab", "'")
}
Output
1
2
3
4
5
6
7
{
"e": "'ab'",
"a": null,
"b": "''",
"c": "xabx",
"d": "''ab''"
}
wrapWith(Null, String): Null
Helper function for wrapWith
to work with null value.