Sub topic 5
Invisible characters TRIM misses: CLEAN and non-breaking spaces
This chapter assumes you can already use TRIM and the Data cleanup > Trim whitespace tool, and that you know what ARRAYFORMULA does. It also assumes you work on a copy of your data, as covered earlier. Here you will find out why a cell can look clean, pass through TRIM, and still fail a lookup, and how to fix it.
Why TRIM sometimes appears to do nothing
TRIM removes one specific character: the ordinary space, character code 32. It strips spaces from the start and end of a cell, and it collapses runs of spaces between words into one.
Many imported values contain other characters that look like spaces or look like nothing at all. TRIM leaves these alone. The most common ones are:
| Character | Code | Typical source | Does TRIM remove it? | Does CLEAN remove it? |
|---|---|---|---|---|
| Space | 32 | Typing | Yes | No |
| Tab | 9 | Text pasted from other tools | No | Yes |
| Line feed | 10 | Multi-line cells, exports | No | Yes |
| Other control characters | 0 to 31 | Databases, legacy systems | No | Yes |
| Non-breaking space | 160 | Web pages (` `), Word, PDFs | No | No |
| Zero-width space | 8203 | Web pages, chat tools | No | No |
| Byte order mark | 65279 | Files saved by some editors | No | No |
The last three rows are the gap in the standard toolkit. CLEAN handles the control characters, TRIM handles ordinary spaces, and nothing built in handles the rest unless you add SUBSTITUTE or REGEXREPLACE.
Diagnose before you fix
Do not guess which character is present. Measure it. Suppose A2 holds the text Jane Doe followed by a non-breaking space, copied from a web page.
Compare the length before and after TRIM:
=LEN(A2) - LEN(TRIM(A2))If this returns 0 but you can see the cell is wrong, for example a lookup on "Jane Doe" fails, then something other than a plain space is the cause.
Next, list the code of every character in the cell. This formula spills one number per character down the column:
=ARRAYFORMULA(UNICODE(MID(A2, SEQUENCE(LEN(A2)), 1)))For Jane Doe plus a trailing non-breaking space, you would see 74, 97, 110, 101, 32, 68, 111, 101, 160. The final 160 is the culprit. Any value other than the letters and the single 32 is worth investigating.
To count suspicious characters across a cell without listing them, use REGEXREPLACE to delete everything in the printable ASCII range and compare lengths:
=LEN(A2) - LEN(REGEXREPLACE(A2, "[^\x20-\x7E]", ""))This returns how many characters fall outside codes 32 to 126.
Replace non-breaking spaces with SUBSTITUTE
A non-breaking space is character 160. Web pages use it to keep two words on the same line, and it is easy to copy into a sheet without noticing. It looks identical to a normal space, but a formula treats it as a different character, so "Jane" & CHAR(160) & "Doe" does not equal "Jane Doe".
The fix is to replace it with a normal space first, and then let TRIM do its usual work:
=TRIM(SUBSTITUTE(A2, CHAR(160), " "))The order matters. If you run TRIM first, the non-breaking spaces survive, and a later SUBSTITUTE leaves you with a normal space at the start or end that nothing removes. Replace first, then trim.
CHAR(160) works in Sheets, but UNICHAR(160) is the clearer choice when you are dealing with Unicode characters, because it states that you mean a Unicode code point. Both give the same result here.
Remove control characters with CLEAN
CLEAN deletes characters with codes 0 to 31. These are non-printing control characters that sometimes arrive from databases, scanned documents, or system exports. They take up room in the cell and break comparisons, but they display as nothing or as a small box.
=CLEAN(A2)CLEAN does not touch character 160, and it does not trim spaces. Its job is narrow.
The line break trap
Tab (9) and line feed (10) are both in the range CLEAN removes. This is useful when a cell contains a stray line break at the end. It causes a problem when the line break is the only separator between two words:
Jane
DoeCLEAN deletes the line feed and returns JaneDoe, with the words joined. If line breaks in your data separate real words, convert them to spaces before you clean:
=TRIM(CLEAN(SUBSTITUTE(SUBSTITUTE(A2, CHAR(160), " "), CHAR(10), " ")))Reading from the inside out, this replaces non-breaking spaces with spaces, replaces line feeds with spaces, removes any remaining control characters, and then trims. If a line break only ever appears at the end of a value, you can skip the CHAR(10) step.
Zero-width characters and REGEXREPLACE
Zero-width characters take up no visible width, so you cannot spot them even by selecting text in the cell. The two you will meet most are the zero-width space (8203) and the byte order mark (65279). Neither is removed by TRIM or CLEAN, and a simple SUBSTITUTE works but needs one call per character.
REGEXREPLACE can strip several at once. Sheets uses the RE2 regular expression engine, which writes a character by its code point as \x{...} with the code in hexadecimal. The zero-width space is 200B and the byte order mark is FEFF:
=REGEXREPLACE(A2, "[\x{200B}\x{FEFF}]", "")Note that these characters are removed, not replaced with spaces. They sit inside words or at the edges of values and carry no meaning as separators. If you find more than two kinds of invisible character in your data, add their hexadecimal codes inside the square brackets.
A complete cleaning formula
Now combine the pieces for a whole column. This formula converts non-breaking spaces to spaces, removes zero-width characters, removes control characters, and trims, for every row from A2 down:
=ARRAYFORMULA(TRIM(CLEAN(REGEXREPLACE(SUBSTITUTE(A2:A, CHAR(160), " "), "[\x{200B}\x{FEFF}]", ""))))Put it in the first cell of an empty helper column, not in the column that holds the raw data. The raw column stays untouched, and you can compare the two side by side.
The earlier version of this idea in the course used only SUBSTITUTE, CLEAN, and TRIM:
=ARRAYFORMULA(TRIM(CLEAN(SUBSTITUTE(A2:A, CHAR(160), " "))))That shorter formula covers the most common case, and it is a good default. Add the REGEXREPLACE step only when the diagnostic shows zero-width characters.
Verify the result
After cleaning, repeat the diagnostics on the output column. Do not just look at the cells.
- Run
=LEN(B2)next to=LEN(A2)for a few rows and confirm that the cleaned length is shorter exactly where you expected. - Run the UNICODE listing on one cleaned cell and confirm that only expected codes remain.
- Run the real task that failed before, such as a VLOOKUP, a COUNTIF, or a pivot grouping, and confirm that it now matches.
- Check that numbers did not turn into text problems. A value like
1,200with a hidden space in it is text. After cleaning, it is still text. Wrap the result in VALUE if you need a number, as in=VALUE(TRIM(SUBSTITUTE(A2, CHAR(160), " "))).
Common mistakes
- Running TRIM alone and concluding that the data is clean because the cell looks the same.
- Running CLEAN on multi-line text and silently joining words.
- Trimming before replacing character 160, which leaves a normal space behind.
- Removing every non-ASCII character and deleting real accented letters.
- Testing only the first few rows. Invisible characters often appear in a handful of rows, so compare the LEN of the whole column, for example with
=SUM(ARRAYFORMULA(LEN(A2:A)))against the same sum for the cleaned column. - Trusting a menu tool without checking. The Trim whitespace menu command is convenient, but confirm with LEN whether it removed the non-breaking spaces in your data instead of assuming it did.
Exercise
Cell A2 contains the text "Acme Corp" with a non-breaking space between the two words and one more at the end. =LEN(A2) returns 10 and =LEN(TRIM(A2)) also returns 10. Write one formula that returns "Acme Corp" with a single normal space, and explain why TRIM alone did not change the length.
Show answer
Use =TRIM(SUBSTITUTE(A2, CHAR(160), " ")). TRIM only removes the ordinary space, character code 32. The non-breaking space is code 160, so TRIM treated both as regular characters and left the length at 10. SUBSTITUTE first turns each character 160 into a code 32 space. TRIM then removes the trailing space and leaves the single space between the words, so the result is "Acme Corp" with length 9.
Exercise
A cell contains "Jane", a line feed, and "Doe". You apply =CLEAN(A2) and get "JaneDoe". How do you get "Jane Doe" instead?
Show answer
CLEAN removes character 10 (line feed) without leaving a space. Replace the line feed with a space before cleaning: =TRIM(CLEAN(SUBSTITUTE(A2, CHAR(10), " "))). The SUBSTITUTE step turns the line break into a space, CLEAN removes any other control characters, and TRIM guards against doubled spaces.