AMPscript Trim (Next)
Strip leading and trailing whitespace with AMPscript Trim in Marketing Cloud Next. Syntax, examples, and behaviour proven in a Marketing Cloud Next email, including how it differs from Engagement.
Syntax
Trim(sourceString) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sourceString |
string | date | Yes | Value to trim |
Exactly one argument. A number or boolean literal breaks the render; quote numeric values.
Example
%%=Trim(Concat(Char(32), Char(32), "hello", Char(32), Char(32)))=%%
Renders hello.
A number written as text trims fine:
%%=Trim(Concat(Char(32), Char(32), "42", Char(32), Char(32)))=%%
Renders 42.
Return value
string — the value with whitespace removed from both ends.
The result is free text, so there is no fixed set of special values to test for.
Behaviour
Write runs of spaces with Char(32). The Next render collapses a run of spaces typed into the email to one space before AMPscript runs: Length("a b") gives 3, while Length(Concat("a", Char(32), Char(32), "b")) gives 4. Build padding or inner runs with Char(32) when the exact spacing matters.
Leading and trailing spaces go. Trim(Concat(Char(32), Char(32), "hi there", Char(32), Char(32))) gives hi there, and Trim("noPad") comes back unchanged.
Runs of inner spaces are kept. Two spaces, a, two spaces, b, two spaces, all built with Char(32), trim to a b with Length 4, as on Engagement.
Empty and all-space values give an empty string. Trim("") renders nothing, and so does five Char(32) in a row; Length of that trimmed value is 0.
Tab, line feed and carriage return count as whitespace. A single letter padded with Char(9), Char(10), Char(13), or a mix of them and spaces, measures Length 1 after trimming.
The non-breaking space is not stripped. Length(Trim(Concat(Char(160), "x", Char(160)))) is 3: both Char(160) characters survive. Remove them first with Replace if your data contains them.
Non-ASCII text inside the value is kept. Trimming éàü padded with two Char(32) on each side gives éàü with Length 3.
Dates are written in ISO form. Trim(Now()) gives a value like 2026-11-22T21:03:22.
Numbers and booleans break the render. The email saves, but the preview fails for Trim(12345) and Trim(true).
One argument only. Trim() and a call with two arguments cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”.
Show Next test script
<!-- Marketing Cloud Next test script for Trim. Paste it into the HTML component of a
Next email and open the preview. Each line prints the result Next gave when the call
was rendered on its own (expect) next to the result of this run (got).
Assembled from single-call test emails (dates on the page); it has not been
run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[noPad] got=[%%=Trim("noPad")=%%]</p>
<p>c2 expect=[] got=[%%=Trim("")=%%]</p>
<p>c3 expect=[1] got=[%%=Length(Trim(Concat(Char(9), "x", Char(9))))=%%]</p>
<p>c4 expect=[1] got=[%%=Length(Trim(Concat(Char(10), "x", Char(10))))=%%]</p>
<p>c5 expect=[1] got=[%%=Length(Trim(Concat(Char(13), "x", Char(13))))=%%]</p>
<p>c6 expect=[1] got=[%%=Length(Trim(Concat(Char(9), Char(10), " x ", Char(13), Char(9))))=%%]</p>
<p>c7 expect=[3] got=[%%=Length(Trim(Concat(Char(160), "x", Char(160))))=%%]</p>
<p>c8 expect=[2026-11-22T21:03:22] got=[%%=Trim(Now())=%%]</p>
<p>c9 expect=[3] got=[%%=Length("a b")=%%]</p>
<p>c10 expect=[a_b] got=[%%=Replace("a b", " ", "_")=%%]</p>
<p>c11 expect=[a_b] got=[%%=Replace("a b", " ", "_")=%%]</p>
<p>c12 expect=[4] got=[%%=Length(Concat("a", Char(32), Char(32), "b"))=%%]</p>
<p>c13 expect=[a__b] got=[%%=Replace(Trim(Concat(Char(32), Char(32), "a", Char(32), Char(32), "b", Char(32), Char(32))), " ", "_")=%%]</p>
<p>c14 expect=[a_b] got=[%%[ SET @r3s6 = "a b" ]%%%%=Replace(@r3s6, " ", "_")=%%]</p>
<p>c15 expect=[x y] got=[x y]</p>
<p>c16 expect=[hi there] got=[%%=Trim(Concat(Char(32), Char(32), "hi there", Char(32), Char(32)))=%%]</p>
<p>c17 expect=[a b] got=[%%=Trim(Concat(Char(32), Char(32), "a", Char(32), Char(32), "b", Char(32), Char(32)))=%%]</p>
<p>c18 expect=[] got=[%%=Trim(Concat(Char(32), Char(32), Char(32), Char(32), Char(32)))=%%]</p>
<p>c19 expect=[hello] got=[%%=Trim(Concat(Char(32), Char(32), "hello", Char(32), Char(32)))=%%]</p>
<p>c20 expect=[4] got=[%%=Length(Trim(Concat(Char(32), Char(32), "a", Char(32), Char(32), "b", Char(32), Char(32))))=%%]</p>
<p>c21 expect=[0] got=[%%=Length(Trim(Concat(Char(32), Char(32), Char(32), Char(32), Char(32))))=%%]</p>
<p>c22 expect=[éàü] got=[%%=Trim(Concat(Concat(Char(32), Char(32)), Char(233), Char(224), Char(252), Concat(Char(32), Char(32))))=%%]</p>
<p>c23 expect=[3] got=[%%=Length(Trim(Concat(Concat(Char(32), Char(32)), Char(233), Char(224), Char(252), Concat(Char(32), Char(32)))))=%%]</p>
<p>c24 expect=[42] got=[%%=Trim(Concat(Char(32), Char(32), "42", Char(32), Char(32)))=%%]</p>
Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
would stop the whole preview or block the save. -->
<!-- email 1: expect that the email saves, but the preview never shows the new version (no fresh render) -->
%%=Trim(12345)=%%
<!-- email 2: expect that the email saves, but the preview fails (HTTP 500) -->
%%=Trim(true)=%%
<!-- email 3: expect that the save is rejected (HTTP 400) -->
%%=Trim()=%%
<!-- email 4: expect that the save is rejected (HTTP 400) -->
%%=Trim(Concat(Char(32), Char(32), "a", Char(32), Char(32)), Concat(Char(32), Char(32), "b", Char(32), Char(32)))=%%
Differs from official docs
- The non-breaking space survives. The reference says whitespace is removed from both ends; in Next
Char(160)at either end is kept. Card - Numbers and booleans break the render. The reference does not rule them out, but
Trim(12345)andTrim(true)made the Next preview fail. Card
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Yes, from API 67.0 |
Engagement strips the non-breaking space; Next leaves Char(160) in place. Engagement also accepts numbers (Trim(12345) gives 12345) and returns an empty string for a boolean, while in Next both make the email preview fail. Spaces are handled the same: both keep runs of inner spaces (a b, length 4) and strip leading and trailing runs. In a Next email, write runs of spaces with Char(32), because the render collapses runs typed into the email before AMPscript runs. Compared 2026-11-23 by rendering the same calls in a Marketing Cloud Next email.
See also
Length— check whether a trim removed anythingReplace— remove characters Trim keeps, such as the non-breaking space- Differs from official docs (Next)
- Official reference