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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `prefix` | the text used as prefix.
|===
Example

This example shows how the appendIfMissing behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import appendIfMissing from dw::core::Strings
---
{
  "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.

Parameters
Name Description

text

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
%dw 2.0
import * from dw::core::Strings
output application/json
---
{ "camelize" : camelize("customer_first_name") }
Output
1
{ "camelize" : "customerFirstName" }

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

text

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

text

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

content

The input string.

position

The index (a Number type) of a character in the string (as a string array). Note that the index of the first character is 0.

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

text

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
{
  "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

charCode

The input Unicode (a Number).

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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isNumeric behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import isAlpha from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isAlphanumeric behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import isAlphanumeric from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isNumeric behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
import isLowerCase from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isNumeric behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%dw 2.0
import isNumeric from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isNumeric behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
import isUpperCase from dw::core::Strings
---
{
  "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 it works with null value.

isWhitespace

isWhitespace(String): Boolean

Checks if the text contains only whitespace.

Since Version: 2.2.0 ===== Parameters

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
|===
Example

This example shows how the isWhitespace behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import isWhitespace from dw::core::Strings
---
{
  "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

text

The input string.

size

The size to pad to.

padText

The text to pad with. It defaults to one space if not specified

Example

This example shows how the leftPad behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
import leftPad from dw::core::Strings
 ---
 {
   "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

num

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

text

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 start of the string if the text does not already start with it

Since Version: 2.2.0 ===== Parameters

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `prefix` | the text used as prefix.
|===
Example

This example shows how the prependIfMissing behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import prependIfMissing from dw::core::Strings
---
{
  "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 amount of specified times

Since Version: 2.2.0 ===== Parameters

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `times` | number of times to repeat char, negative treated as zero
|===
Example

This example shows how the repeat behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
import repeat from dw::core::Strings
---
{
  "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

text

The input string.

size

The size to pad to.

padText

The text to pad with. It defaults to one space if not specified

Example

This example shows how the rightPad behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
import leftPad from dw::core::Strings
---
{
  "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

text

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
[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `separator` | String to search for.
|===
Example

This example shows how the substringAfter behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
import substringAfter from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `separator` | String to search for.
|===
Example

This example shows how the substringAfterLast behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
import substringAfterLast from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `separator` | String to search for.
|===
Example

This example shows how the substringBefore behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
import substringBefore from dw::core::Strings
---
{
  "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
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
  "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

 [%header, cols="1,3"]
 |===
 | Name | Description
 | `text` | The input string.
 | `separator` | String to search for.
 |===

===== Example

This example shows how the `substringBeforeLast` behaves under different inputs and sizes.

====== Source

[source,DataWeave, linenums]

import substringBeforeLast from dw::core::Strings --- { "a": substringBeforeLast(null, "'"), "b": substringBeforeLast("", "-"), "c": substringBeforeLast("abc", "b"), "d": substringBeforeLast("abcba", "b"), "e": substringBeforeLast("abc", "d"), "f": substringBeforeLast("abc", "") }

====== Output
[source,JSON,linenums]

{ "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

text

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
{
   "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `wrapper` | the text used to unwrap
|===
Example

This example shows how the unwrap behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
%dw 2.0
import unwrap from dw::core::Strings
---
{
  "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

[%header, cols="1,3"]
|===
| Name | Description
| `text` | The input string.
| `wrapper` | The content used to wrap.
|===
Example

This example shows how the wrapIfMissing behaves under different inputs and sizes.

Source
1
2
3
4
5
6
7
8
9
10
11
12
13
%dw 2.0
 import wrapIfMissing from dw::core::Strings
 ---
 {
   "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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
   "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

 [%header, cols="1,3"]
 |===
 | Name | Description
 | `text` | The input string.
 | `wrapper` | The content used to wrap.
 |===

===== Example

This example shows how the `wrapWith` behaves under different inputs and sizes.

====== Source

[source,DataWeave, linenums]

import wrapWith from dw::core::Strings --- { "a": wrapWith(null, "'"), "b": wrapWith("", "'"), "c": wrapWith("ab", "x"), "d": wrapWith("'ab'", "'"), "e": wrapWith("ab", "'") }

====== Output
[source,JSON,linenums]

{ "e": "'ab'", "a": null, "b": "''", "c": "xabx", "d": "''ab''" } ----

wrapWith(Null, String): Null

Helper function for wrapWith to work with null value.