Text functions
TRIM
Matches all 5 recorded answers
Recorded: 5 workbook cells. How we measure this · See the Rust code
Local names in 40 languages
| Language | Name in Excel |
|---|---|
| Arabic | TRIM |
| Bulgarian | TRIM |
| Czech | TRIM |
| Danish | TRIM |
| German | TRIM |
| Greek | TRIM |
| English (Australia) | TRIM |
| English (United Kingdom) | TRIM |
| English (United States) | TRIM |
| Spanish | TRIM |
| Estonian | TRIM |
| Finnish | POISTA.VÄLIT |
| French | SUPPRESPACE |
| Hebrew | TRIM |
| Croatian | TRIM |
| Hungarian | TRIM |
| Indonesian | TRIM |
| Italian | TRIM |
| Japanese | TRIM |
| Korean | TRIM |
| Lithuanian | TRIM |
| Latvian | TRIM |
| Norwegian | TRIM |
| Dutch | TRIM |
| Polish | TRIM |
| Portuguese (Brazil) | TRIM |
| Portuguese (Portugal) | TRIM |
| Romanian | TRIM |
| Russian | TRIM |
| Slovak | TRIM |
| Slovenian | TRIM |
| Serbian (Latin) | TRIM |
| Swedish | TRIM |
| Thai | TRIM |
| Turkish | KIRP |
| Ukrainian | TRIM |
| Vietnamese | TRIM |
| Chinese (Simplified) | TRIM |
| Chinese (Hong Kong) | TRIM |
| Chinese (Traditional) | TRIM |
Names from Microsoft's support site.
What it does
TRIM(text)
TRIM is not the trim you know from other languages. Most languages' trim removes leading and
trailing whitespace and leaves the interior alone. Excel's TRIM also collapses interior runs.
The rule, stated exactly: take the text, delete every leading space, delete every trailing space, and replace each remaining run of consecutive spaces with a single space. Equivalently — and this is the formulation to implement — split the text on spaces, discard empty pieces, and rejoin with one space between pieces.
Two things follow that catch people out:
TRIMis not idempotent-by-accident, it is idempotent by construction.TRIM(TRIM(x))isTRIM(x), because the output contains no leading, trailing, or doubled spaces by definition.TRIMchanges the interior of your data. If your text is fixed-width or contains deliberate alignment padding,TRIMdestroys it.SUBSTITUTE(x, " ", "")removes spaces;TRIMnormalises them; they are different tools and neither is a general "clean up" function.
Arguments
TRIM(text)
| Argument | Required | Meaning |
|---|---|---|
text | yes | The text to normalise. |
There is exactly one argument and no options. There is no way to ask TRIM to strip the ends
without collapsing the interior, and no way to widen its notion of whitespace.
Non-text arguments convert to text first, by the ordinary rules in
coercion and lifting: a number arrives as its general-format
rendering, TRUE as the string TRUE. Since none of those renderings contains a space that a
naïve reading would expect to survive, TRIM of a non-text scalar is generally that scalar's text
form unchanged.
Try it
Quirks
Result and edge cases
The return kind is Text. TRIM of the empty string is the empty string; TRIM of a string of
nothing but spaces is the empty string.
Other boundaries:
- An empty referenced cell delivers
Empty, distinct from an omitted argument; see the value universe. - Errors propagate.
TRIMhas no error-masking policy. - Astral characters are unaffected, but note that the model's text is a sequence of UTF-16
code units, so a
TRIMimplemented over code units and one implemented over Unicode scalars will agree here only because no space character is astral. Do not generalise from that to the rest of the text family.
Errors
Microsoft's page documents no error conditions for TRIM beyond the ordinary ones (see Sources).
In practice the reachable errors are:
| Error | Condition |
|---|---|
| any error value | Propagated from the argument; coercion does not discard worksheet errors. |
#VALUE! | An argument whose kind has no text conversion in this context. |
The Handbook has not established which argument kinds fall in that second row for TRIM
specifically.
How to implement
Notes for implementers
- The character set is the specification. Implement
TRIMagainst an explicit list of code points, not against a library'sis_whitespacepredicate. A Unicode-awareis_whitespacematches tab, newline, non-breaking space, en quad, ideographic space and more; the evidence recorded upstream says the non-breaking space at least survives Excel'sTRIM. Using the language's default whitespace class here is the single most likely way to diverge. - Collapse, don't just strip. A port that reuses a host-language
trim()produces wrong interior results for every string with a double space. - Tabs and newlines are a separate question from the non-breaking space, and this Handbook does not have an answer for them. Do not assume they behave like the space just because the language you are porting from groups them together.
- The 32,767-code-unit text cap applies to the result as to any string; see
the value universe.
TRIMonly ever shortens, so it cannot create a cap violation, but it can be the step that makes an over-long input fit. - Lift a single array-valued argument rather than rejecting arrays outright — that is the behaviour the upstream record pins.
Rust code
Show the CalcFunc source of this function
Quoted from the public repository DnaCalc/DnaCalc, crates calc-func and calc-fml, at commit 9e1884369a67. Comments are the source's own. A kernel shared with other functions appears once, on its module page.
TRIM · 4 lines · calc-func/src/catalogue/text_functions.rs on GitHub16 trim_fn "TRIM" (1, 1) [V "text"] |s, a, sv| {
let text = text_arg(s, a, 0)?;
Ok(CalcValue::text(k::trim_ascii_spaces(&text)))
}
trim_ascii_spaces · 20 lines · calc-func/src/ported/text_kernels.rs on GitHubpub(crate) fn trim_ascii_spaces(text: &ExcelText) -> ExcelText {
let mut out = Vec::new();
let mut pending_space = false;
let mut started = false;
for unit in text.utf16_code_units() {
if *unit == 32 {
if started {
pending_space = true;
}
continue;
}
if pending_space && !out.is_empty() {
out.push(32);
}
out.push(*unit);
started = true;
pending_space = false;
}
ExcelText::from_utf16_code_units(out)
}
Kernels shared with other functions
textported/text_kernels
Argument and result plumbing
text_argcatalogue/kit
No implementation has been published with a Handbook test suite for this function.
Version differences
No comparison between specific Excel builds is recorded here yet. This does not establish that all builds behave alike.
Alternatives
Relationships
CLEAN— the neighbouring "tidy the text" function, and the oneTRIMis most often confused with.CLEANremoves non-printable control characters;TRIMnormalises spaces. Neither does the other's job, andTRIM(CLEAN(x))is the common pairing.SUBSTITUTE— removes or replaces a specified character. This is the escape hatch for whitespaceTRIMwill not touch, notablyUNICHAR(160).TEXTSPLITwithignore_emptyperforms the same "drop empty pieces" operation on a general delimiter, which isTRIM's rule in generalised form.TEXTJOINwithignore_emptyset is the rejoining half of the same idea.TRIMRANGEand the trim-reference operators are not related despite the name: they trim empty rows and columns from a reference, not spaces from a string.EXACTand=comparisons are where untrimmed data usually announces itself, which is whyTRIMshows up so often in lookup keys.
Named variants
No named variants are recorded yet.
Suggest a correction or contribute an implementation · For agents