Formatting · Cleanup · Data

Remove Extra Spaces

Clean up messy spacing in any text instantly.

Input
Output. Cleaned
Spaces removed: 0
Characters before: 0
Characters after: 0
Advertisement

About the Remove Spaces tool

Strip whitespace from text. All spaces, multiple spaces collapsed to one, leading and trailing spaces only, tabs only, line breaks only, or all whitespace including invisible characters. Six modes cover every real cleanup case. Everything runs locally in your browser; nothing is uploaded.

What "remove spaces" can mean

"Remove spaces from this text" sounds unambiguous until you try to use a generic spaces-removal tool on real data and watch it produce wrong results. There are at least six distinct operations that get called "remove spaces," and they produce different output:

Remove all spaces. Every space character is deleted. "hello world" becomes "helloworld". Useful for converting to a no-space identifier, but destructive. You lose all word boundaries.

Collapse multiple spaces to one. Runs of two or more spaces become a single space. "hello   world" becomes "hello world". The most common cleanup operation, since copy-paste often introduces extra spaces that look fine visually but break downstream tools.

Trim leading and trailing only. Spaces at the start and end of each line are removed; internal spaces are preserved. Useful for cleaning copy-pasted lists where each line might have unwanted indentation or trailing whitespace.

Remove tabs only. Tab characters are deleted (or replaced with a single space). Useful for converting tab-separated content into space-separated, or for normalizing inconsistent indentation.

Remove line breaks only. Newline characters are deleted, joining all lines into one. Useful for collapsing multi-line content into a single string for pasting into form fields that don't accept multi-line input.

Remove all whitespace. Every whitespace character (spaces, tabs, line breaks, vertical tabs, form feeds, plus invisible Unicode whitespace like the no-break space and the zero-width space) is deleted. The most aggressive option; usually overkill but occasionally necessary.

This tool exposes all six as separate modes so you can pick the one that matches the job.

Real use cases

Cleaning OCR output. Optical character recognition produces text with sporadic extra spaces from imperfect character segmentation. A scan of a printed page often comes back with double spaces, missing spaces, and trailing tabs that look fine in some viewers and broken in others. Collapse-multiple-spaces is the right first pass; trim-leading-and-trailing is the right second pass.

Fixing copy-pasted URLs. URLs copied from email or chat often include a space or non-breaking space at the start or end. Pasted into a browser bar, they fail with cryptic errors. Run through this tool with trim-only to fix.

Normalizing email addresses. Form submissions sometimes capture leading or trailing spaces in email fields (" example@gmail.com" or "example@gmail.com "). These look identical to clean addresses to the human eye but fail validation against most email regexes. Trim-only on the field value catches this.

Cleaning CSV data. Spreadsheet exports sometimes contain hidden trailing spaces in cells, especially if the source data came from human-typed forms. These spaces survive the export and break downstream string comparisons. Trim is the right cleanup; full-whitespace-removal is the wrong one (it would join "John Smith" into "JohnSmith").

Compressing text for tweets, SMS, or any character-limited field. Removing extra spaces (collapse mode) recovers a few characters per sentence and can be the difference between fitting and not fitting in a 280-character window. Aggressive whitespace removal is sometimes used for the same purpose, though it makes text unreadable.

Generating identifiers from natural-language strings. "Hello World" → "HelloWorld" by removing all spaces is a common preprocessing step before further case conversion. Pair this tool with the Case Converter for full identifier generation.

Preparing text for password fields, API keys, or tokens. Many systems strip leading and trailing whitespace silently; others don't. Defensively running input through a trim before submission prevents the "but I copy-pasted exactly the right value" support-ticket category.

Hidden whitespace characters

Beyond the obvious space, tab, and newline, real-world text contains invisible characters that look like whitespace but defeat naive removal. The tool's "all whitespace" mode handles them; the targeted modes do not.

Non-breaking space (U+00A0). Looks like a space, prevents line wrapping. Common in HTML-derived text and Microsoft Word output. Keyboard shortcut Option+Space on Mac produces it.

Zero-width space (U+200B). Invisible. Used by some CMSes to insert tracking pixels or to hint line-break positions. Defeats deduplication and equality checks.

Em space, en space, hair space, thin space (U+2003, U+2002, U+200A, U+2009). Different widths, all invisible at small sizes. Common in typeset text from LaTeX or InDesign exports.

Zero-width joiner and non-joiner (U+200D, U+200C). Used in some scripts (Devanagari, Persian) for ligature control; sometimes appear in copy-pasted emoji sequences.

Byte Order Mark (U+FEFF). A single invisible character at the start of files saved with certain encodings. Defeats the very first character of equality comparisons.

If you're dealing with text where "remove spaces" doesn't seem to be removing what you can see, switch to the all-whitespace mode and try again. One of these invisible characters is probably the culprit.

Common pitfalls

Removing all spaces destroys word boundaries. Don't use the all-spaces mode on natural-language text unless you have a specific reason. The collapse-multiple mode is almost always what you actually want.

Trim doesn't fix internal whitespace. If a line is "hello   world", trim removes the leading and trailing nothing-special, leaving the triple internal space intact. You probably want collapse mode instead.

Removing newlines turns documents into walls of text. Useful occasionally for paste-into-form scenarios; almost never what you want for general cleanup. Confirm before applying.

Aggressive whitespace removal can corrupt code. Source code, JSON, and YAML are whitespace-significant. Don't run them through any of these modes; use a proper formatter instead (the JSON Formatter for JSON, language-specific tools for code).

Remove Spaces vs Find & Replace vs regex

This tool. Fastest for the six common modes, no regex syntax to remember, handles invisible Unicode whitespace in all-mode.

Find & Replace. Necessary when you need to remove a specific character that isn't whitespace, when you need to match by context (only spaces around commas, only spaces inside quoted strings), or when you need targeted single-instance replacement rather than global removal.

Regex. Necessary when whitespace removal must be conditional on what surrounds it. \s+ in regex matches any whitespace; combinations like \s+(?=[,.]) match whitespace only before punctuation. For complex conditional cleanup, a regex tool is the right answer.

How the tool works

The tool applies a JavaScript regular expression matching the whitespace pattern you select, replacing matches with either an empty string or a single space depending on the mode. For the all-whitespace mode, the regex covers the entire Unicode whitespace class plus the zero-width and BOM characters that aren't technically whitespace but behave like it for cleanup purposes.

Performance is linear in input size. Multi-megabyte text runs in well under a second.

Workflow tips

Default to "collapse multiple spaces." It fixes 80% of real cleanup needs without destructive side effects. Reach for the more aggressive modes only when collapse doesn't get the job done.

Always trim email addresses on capture. If you control the form, trim leading/trailing whitespace before validation. If you don't, run captured emails through this tool's trim mode before further processing.

Audit invisible characters with the Character Counter first. If a line "looks fine" but isn't matching, paste it into the Character Counter and check the count against what you expect. A mismatch points to invisible characters that targeted whitespace removal won't catch.

Frequently asked questions

What counts as "whitespace"?

In the targeted modes (spaces only, tabs only, newlines only), only the literal character you select. In the "all whitespace" mode, the full Unicode whitespace category plus the zero-width characters and the byte order mark. Anything that's invisible or behaves like whitespace, even if technically classified differently.

Will it preserve line breaks?

By default, yes. Only the "remove line breaks only" and "remove all whitespace" modes affect newlines. The other four modes preserve line structure.

Does it handle non-breaking spaces?

The "all whitespace" mode does. The "remove all spaces" mode (which targets only the regular ASCII space, U+0020) does not. If your text contains non-breaking spaces masquerading as regular ones, use the all-whitespace mode.

Why didn't it remove a space I can see?

Almost certainly an invisible-but-look-like-space character. Try the all-whitespace mode. If it still doesn't catch it, paste the input into the Character Counter to identify what character is actually there.

Can I keep some spaces and remove others?

Not with this tool. Conditional whitespace removal. Like "remove spaces only between digits". Requires regex matching with context, which is what the Find & Replace tool is for.

Does it modify newlines on Windows differently than on Mac?

The tool treats both \n (Unix/Mac) and \r\n (Windows) as line breaks, so the behavior is consistent regardless of where the text originated. The output uses Unix-style \n by default.

Related

Advertisement

Learn more about remove extra spaces