Runtime verified Test scripts included

Syntax

Trim(sourceString)  →  string
1 argument — exactly

Parameters

Name Type Required Description
sourceString string | number | date Yes Value to trim

Example

%%[
  VAR @name
  SET @name = Trim("  hello  ")
]%%
%%=v(@name)=%%

Renders hello.

The everyday use is cleaning a field before storing or comparing it, where the padding is invisible in the source data:

%%[
  VAR @city
  SET @city = Trim(AttributeValue("City"))
]%%

Return value

string — the value with whitespace removed from both ends.

The result is arbitrary trimmed text, so there is no closed set of sentinel values to test for.

Behaviour

Only the outer edges are touched. Trim(" a b ") gives a b — the two inner spaces survive. This never collapses or normalises spacing inside the value.

A value with no padding comes back unchanged. Trim("noPad") gives noPad.

An empty string and an all-whitespace string both return an empty string. Trimming five spaces measured Length 0.

Numbers and dates are accepted and handled as their string form. Trim(12345) gives 12345, and Trim(Now()) returns the formatted date. A numeric string trims like any other string — Trim(" 42 ") gives 42.

Booleans are swallowed silently. Trim(true) returns HTTP 200 with an empty result, so this is a rejection rather than boolean support and the parameter type stays string | number | date. The same happens across the String family; see Differs from official docs.

Whitespace is more than the space character

The tab (9), line feed (10), carriage return (13) and the non-breaking space (160) are all stripped. Each case padded a single letter and measured the result, because the characters are invisible in rendered output:

Padding around one letter Length after trimming
tab 1
line feed 1
carriage return 1
non-breaking space 1
mixed tab, line feed, space and carriage return 1

The non-breaking space matters most in practice: it survives most copy-paste cleanup and looks identical to a space, so a value that appears untrimmable is often padded with it. Non-ASCII characters inside the value are preserved exactly — an accented three-letter payload came back with its codepoints unchanged and Length 3.

Show test script
%%[
  VAR @b, @acc
  SET @b = RequestParameter("b")
  SET @acc = Concat(Char(233), Char(224), Char(252))

  /* documented form: leading and trailing spaces removed, inner spacing kept */
  IF @b == "safe" THEN
    OutputLine(Concat("T1=[", Trim("  hello  "), "]"))
    OutputLine(Concat("T2=[", Trim("  a  b  "), "]"))
    OutputLine(Concat("T3=[", Trim("noPad"), "]"))
    OutputLine(Concat("TEMPTY=[", Trim(""), "]"))
    OutputLine(Concat("TWSONLY=[", Trim("     "), "]"))
    OutputLine(Concat("LENTWSONLY=[", Length(Trim("     ")), "]"))
  ENDIF

  /* whitespace is broader than the space character - measure, do not eyeball */
  IF @b == "ws" THEN
    OutputLine(Concat("LENTAB=[", Length(Trim(Concat(Char(9), "x", Char(9)))), "]"))
    OutputLine(Concat("LENNL=[", Length(Trim(Concat(Char(10), "x", Char(10)))), "]"))
    OutputLine(Concat("LENCR=[", Length(Trim(Concat(Char(13), "x", Char(13)))), "]"))
    OutputLine(Concat("LENNBSP=[", Length(Trim(Concat(Char(160), "x", Char(160)))), "]"))
    OutputLine(Concat("LENMIX=[", Length(Trim(Concat(Char(9), Char(10), " x ", Char(13), Char(9)))), "]"))
  ENDIF

  /* non-ASCII payload survives; read these as codepoints, not on a console */
  IF @b == "nau" THEN
    OutputLine(Concat("ECHO=[", @acc, "]"))
    OutputLine(Concat("TNAU=[", Trim(Concat("  ", @acc, "  ")), "]"))
    OutputLine(Concat("LENTNAU=[", Length(Trim(Concat("  ", @acc, "  "))), "]"))
  ENDIF

  /* numbers and dates are accepted and handled as their string form */
  IF @b == "tnum" THEN
    OutputLine(Concat("TNUM=[", Trim(12345), "]"))
    OutputLine(Concat("TNUMSTR=[", Trim("  42  "), "]"))
  ENDIF

  IF @b == "tdate" THEN
    OutputLine(Concat("TDATE=[", Trim(Now()), "]"))
  ENDIF

  /* a boolean returns HTTP 200 but renders nothing */
  IF @b == "tbool" THEN
    OutputLine(Concat("TBOOL=[", Trim(true), "]"))
  ENDIF

  /* both branches below abort the page: the start marker never renders */
  IF @b == "t0" THEN
    OutputLine(Concat("--- t0 start ---"))
    OutputLine(Concat("T0=[", Trim(), "]"))
  ENDIF

  IF @b == "t2" THEN
    OutputLine(Concat("--- t2 start ---"))
    OutputLine(Concat("T2A=[", Trim("  a  ", "  b  "), "]"))
  ENDIF
]%%

Availability

Platform Available
Marketing Cloud Engagement Yes
Marketing Cloud Next Yes, from API 67.0

See also