Trim Spaces Between Words: How to Fix Spacing
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
Trimming spaces between words means removing extra, unwanted spaces so that each word is separated by exactly one space. This article explains what causes extra spaces, how to remove them in common tools, and how to avoid them in the future.
Formula
The core operation is a simple find-and-replace: replace every occurrence of two consecutive spaces with a single space. In formula terms, if your original text is T, the trimmed text T’ is obtained by repeatedly applying the substitution T’ = replace(T, ” “, ” “) until no double spaces remain. This is equivalent to using a regular expression like \s{2,} replaced with a single space, which also collapses tabs and newlines.
Worked Example
Using the default text “This is a test text”:
- Start:
This is a test text - Replace the first double space after “This”:
This is a test text - Replace the triple space after “is”: first replace two spaces, then the remaining one becomes a double space, then replace again:
This is a test text - Replace the four spaces after “test”: first two, then two more, then the final double:
This is a test text
The result is “This is a test text” with single spaces between every word.
Why Extra Spaces Appear
Extra spaces usually come from copying text from PDFs, web pages, or emails that use justified alignment, or from typing with the spacebar held down too long. Sometimes, non-breaking spaces (Unicode U+00A0) or tabs are used instead of regular spaces, which can be harder to spot. These invisible characters can cause alignment issues in documents and make text look unprofessional.
How to Trim Spaces in Different Tools
Almost every text editor, word processor, and spreadsheet application has a find-and-replace feature. The general approach is:
- Open the Find and Replace dialog (usually Ctrl+H on Windows, Cmd+H on Mac).
- In the “Find what” field, type two spaces (or paste a tab character if needed).
- In the “Replace with” field, type a single space.
- Click “Replace All” repeatedly until no more replacements are made.
In Microsoft Word, you can also use the Find and Replace with wildcards to target multiple spaces at once. In Google Docs, the same find-and-replace works, but you may need to copy and paste a tab character from another source. In Excel, the TRIM function removes extra spaces, including leading and trailing spaces, but it also removes line breaks, so use it carefully.
Common Mistakes and How to Avoid Them
A frequent mistake is replacing only double spaces once, which leaves triple or quadruple spaces partially collapsed. Always repeat the replacement until the count of replacements is zero. Another mistake is using a regular space to replace a non-breaking space—this can break words that should stay together (like a number and its unit). To avoid this, use the Unicode-aware replacement in your editor, or manually replace the non-breaking space character with a regular space.
Also, be aware that some tools treat tabs and spaces differently. If you want to convert all tabs to single spaces, replace the tab character (often shown as \t) with a space, then collapse multiple spaces as above.
When to Trim Spaces (and When Not To)
Trim spaces when you are cleaning up pasted text for a document, preparing data for a database, or formatting code where indentation matters. However, do not trim spaces in situations where multiple spaces are intentional, such as in poetry, ASCII art, or when aligning columns in plain text. In programming, trimming spaces from user input is common, but you should also trim leading and trailing spaces using functions like trim() in most languages.
Alternatives to Manual Trimming
If you have a large document, manual find-and-replace can be tedious. Alternatives include:
- Using a regular expression in a text editor like Notepad++ or VS Code: search for
\s{2,}and replace with a single space. - Using a command-line tool like
sedon Linux/Mac:sed -i 's/ */ /g' file.txt(note the two spaces and asterisk). - Using a dedicated text cleaner website that can also remove extra line breaks and trim leading/trailing spaces.
For spreadsheets, the TRIM function is the fastest way to clean a column of data.
Frequently Asked Questions
How do I remove extra spaces between words in Word?
Open Find and Replace (Ctrl+H), type two spaces in the “Find what” box and one space in “Replace with”, then click “Replace All” repeatedly until the number of replacements is zero. For multiple spaces at once, check the “Use wildcards” option and search for {2,} with a space before it.
Why does my text have double spaces after pasting?
Pasting from PDFs or web pages often retains the original formatting, including extra spaces used for alignment. Also, some fonts render spaces wider than others, making single spaces look like double. Use the find-and-replace method to clean up the text.
What is the difference between trimming spaces and removing all spaces?
Trimming spaces means reducing multiple spaces to a single space, while removing all spaces would concatenate words (e.g., “hello world” becomes “helloworld”). Trim is what you want for normal text; removing all spaces is only useful for specific data cleaning tasks.
Can I trim spaces in Excel without affecting line breaks?
The TRIM function removes extra spaces and also removes line breaks. To preserve line breaks, use a formula like SUBSTITUTE(SUBSTITUTE(A1, CHAR(10), " "), " ", " ") which first converts line breaks to spaces, then collapses double spaces.
How do I trim spaces in a PDF?
PDFs are not meant for editing, so you should extract the text and clean it in a word processor. Use the find-and-replace method after copying the text, or use a PDF-to-text converter that often has an option to normalize spaces.
Is there a keyboard shortcut to trim spaces?
There is no universal shortcut, but in most editors, Ctrl+H opens find-and-replace, which is the fastest way. In some applications like Notepad++, you can record a macro to do the replacement with a single key.
Related
Formula used
in t | Text with extra spaces | - | This is a test text | 0 out trimmed | Trimmed text | - | replace(t, " ", " ") | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.