Contact Us 1-800-596-4880

withMaxSize

withMaxSize(String, Number): String

Checks that the string length isn’t greater than the specified maxLength. If the string is longer, the function returns a substring that starts at the beginning of the string and is maxLength characters long.

Introduced in DataWeave 2.3.0. Supported by Mule 4.3 and later.

Parameters

Name Description

text

The input string.

maxLength

The maximum length of the string.

Example

This example shows how withMaxSize behaves with different inputs and sizes. Note that if withMaxSize is 0, the function returns an empty string. If the input is null, the output is always null.

Source

%dw 2.0
import withMaxSize from dw::core::Strings
output application/json
---
{
   a: "123" withMaxSize 10,
   b: "123" withMaxSize 3,
   c: "123" withMaxSize 2,
   d: "" withMaxSize 0,
   e: null withMaxSize 23,
}
DataWeave

Output

{
  "a": "123",
  "b": "123",
  "c": "12",
  "d": "",
  "e": null
}
JSON

withMaxSize(Null, Number): Null

Helper function that enables withMaxSize to work with a null value.