Skip to content

How to Remove Extra Spaces and Fix Text Spacing

Why Extra Spaces Appear in Your Text

Extra spaces — double spaces, triple spaces, leading spaces, trailing spaces — are among the most common text quality issues. They appear invisible to casual reading but cause problems in code, data processing, publishing, and professional documents.

Clean up any spacing issue instantly with WritePadPro's Remove Extra Spaces tool — paste your text and collapse all multiple spaces to single spaces, trim leading and trailing whitespace, and produce clean, uniform text.

Understanding where extra spaces come from helps you prevent them and recognize when cleanup is needed.

Copy-Paste From Different Sources

Copying text from PDFs, web pages, email clients, and word processors introduces invisible formatting artifacts. A paragraph copied from a PDF might contain spaces where line breaks were, double spaces between sentences from an older typesetting convention, or non-breaking spaces from the PDF layout engine. Each source adds its own whitespace quirks.

The Double-Space After Periods Habit

Typewriter-era typists were taught to press the spacebar twice after periods. This convention persisted through early word processing but is now considered outdated. Major style guides (APA, Chicago, AP) all recommend single spacing after periods. Yet many documents still contain double spaces, often from writers who were taught the two-space rule.

OCR (Optical Character Recognition)

Text extracted from scanned documents via OCR frequently contains irregular spacing. The OCR software interprets column gaps, justified text spacing, and character spacing as actual space characters. A single page of OCR text might contain dozens of multi-space runs.

Legacy Systems and Data Exports

Fixed-width database fields pad content with trailing spaces to fill the column width. A 50-character "name" field stores "John Smith" followed by 40 spaces. When this data is exported, the padding comes with it. Tab-delimited files converted to space-delimited files can also produce irregular spacing.

Code Editors and Indentation

Mixed tabs and spaces in code create visible alignment issues. Different editors interpret tab width differently — a file indented with tabs at 4-space width looks broken in an editor set to 2-space tabs. Copying code between editors compounds the problem. For finding invisible characters that look like spaces but are not, see our Invisible Characters Guide.

Types of Whitespace Characters

What we casually call "a space" is actually one of many whitespace characters in Unicode. Knowing which type you are dealing with determines the correct cleanup approach.

CharacterUnicodeNameCommon Source
(regular space)U+0020SpaceSpacebar — the standard word separator
(tab)U+0009Horizontal TabTab key — used for indentation
(NBSP)U+00A0Non-Breaking SpaceWord processors, HTML ( ), PDFs
(en space)U+2002En SpaceTypography — width of the letter N
(em space)U+2003Em SpaceTypography — width of the letter M
(thin space)U+2009Thin SpaceTypography — used around dashes
(hair space)U+200AHair SpaceTypography — thinnest visible space
(ZWSP)U+200BZero-Width SpaceWeb content, Unicode text — invisible
(ideographic)U+3000Ideographic SpaceCJK (Chinese/Japanese/Korean) text

WritePadPro's Remove Extra Spaces tool collapses runs of regular spaces (U+0020). With its default options it first converts tabs and Unicode spaces such as NBSP, en, em, thin, hair and ideographic spaces into regular spaces. Zero-width spaces are not converted because they take up no room; find and remove those with the Invisible Character Detector. For line break cleanup, see the Remove Line Breaks tool.

The Problems Extra Spaces Cause

In Code

Extra spaces in code break string comparisons ("hello " !== "hello"), cause indentation inconsistencies that violate linting rules, break YAML files (where indentation is syntactic), corrupt fixed-width data formats, and create visual misalignment in teams using different editor settings.

In Data Processing

Trailing spaces in database fields cause failed lookups — searching for "Smith" will not match "Smith " with a trailing space. Leading spaces in CSV columns create phantom entries. Inconsistent spacing in merge fields produces "Dear John" (double space) in mail merges.

In Publishing

Double spaces after periods look unprofessional in modern typography. They create visible rivers of whitespace in justified text. They inflate character counts, potentially pushing content beyond platform limits. Many editors and publishers remove them as a matter of house style.

In SEO and Web Content

Search engines normalize whitespace when indexing, so extra spaces do not directly affect rankings. However, extra spaces in title tags and meta descriptions waste precious character space. Double spaces in visible content reduce perceived quality. And extra spaces in URLs can cause broken links. For related cleanup, see our Remove Duplicate Lines Guide.

How to Remove Extra Spaces

The Regex Approach

The standard method to collapse multiple spaces to single spaces is a regular expression replacement:

text.replace(/ {2,}/g, " ").trim()

This pattern matches any run of 2 or more consecutive space characters and replaces the entire run with a single space. The .trim() removes leading and trailing spaces.

Language-Specific Methods

LanguageCode
JavaScripttext.replace(/\s+/g, " ").trim()
Python" ".join(text.split())
PHPpreg_replace("/\s+/", " ", trim($text))
Javatext.replaceAll("\\s+", " ").trim()
C#Regex.Replace(text, @"\s+", " ").Trim()
Bashecho "$text" | tr -s " "

Note: \s+ matches all whitespace (spaces, tabs, newlines), while / {2,}/ matches only multiple regular spaces. Choose based on whether you want to normalize all whitespace types or only collapse extra spaces.

The Non-Breaking Space Trap

Standard regex whitespace matching (\s) does not always match non-breaking spaces (U+00A0) in all languages. JavaScript's \s does match NBSP, but PHP's \s does not by default. If your text contains non-breaking spaces (common in content copied from web pages or Word documents), you may need to specifically target them: text.replace(/[\s\u00A0]+/g, " ")

Preventing Extra Spaces

Editor Settings

Configure your text editor to show whitespace characters. Most code editors (such as VS Code and Sublime Text) have a "show whitespace" option that renders spaces as dots and tabs as arrows. This makes extra spaces immediately visible during editing.

Linters and Formatters

Code linters (ESLint, Prettier, Black, RuboCop) automatically enforce consistent spacing. Configure them to run on save or as pre-commit hooks. They catch extra spaces before they enter your codebase.

Paste as Plain Text

When copying text from external sources, use "Paste as plain text" (Ctrl+Shift+V in many applications) to strip formatting artifacts. This habit removes a common source of formatting problems in documents.

Post-Editing Cleanup

After finishing any document, run it through WritePadPro's space remover as a final quality check. This catches spacing issues that accumulated during writing and editing — a quick step that produces cleaner output. For related cleanup steps, see our Invisible Characters Guide.

Using WritePadPro's Remove Extra Spaces Tool

WritePadPro's Remove Extra Spaces tool cleans up spacing issues in any text instantly.

Step 1: Paste Your Text

Open the Remove Extra Spaces tool and paste content from any source — documents, emails, code, data exports, PDFs, web pages.

Step 2: Choose Options and Clean

Five checkboxes, all on by default, control the cleanup: Collapse multiple spaces into one, Convert tabs to spaces, Convert non-breaking & other Unicode spaces, Trim spaces at line starts (untick it to keep code indentation) and Trim spaces at line ends. Click Clean Spaces to apply them. Line breaks are never changed.

Step 3: Review the Results

A live panel counts what the tool finds as you type:

  • Multi-space Runs — places where two or more spaces sit together
  • Tabs — tab characters in the text
  • Non-breaking / Unicode — special space characters
  • Chars to Remove — how many characters the current options will remove

After you click the button, the cleaned text replaces your input and a summary reports how many whitespace characters were removed and how many tabs and special spaces were converted.

Step 4: Copy or Download

Use Copy Result or Download .txt to take the text to your document, code editor, spreadsheet, or publishing platform. With the default options it has no double spaces, leading spaces, or trailing spaces. Undo restores the original.

Privacy

All processing runs locally in your browser. Your text is never transmitted to any server.

Summary

Extra spaces appear from copy-paste operations, OCR processing, the outdated double-space-after-period habit, legacy database padding, and mixed editor settings. They cause problems in code (broken comparisons, lint failures), data processing (failed lookups, corrupt merges), publishing (unprofessional appearance, wasted characters), and web content (broken links, reduced quality perception).

Prevention is better than cleanup: show whitespace in your editor, use linters, paste as plain text, and run a spacing check before publishing. When cleanup is needed, WritePadPro's Remove Extra Spaces tool collapses multiple spaces to singles, converts tabs and special spaces, and trims leading/trailing whitespace — instantly, in your browser, with complete privacy.

Frequently Asked Questions

Should I use one space or two after a period?

One space. The major style guides — APA (7th edition), Chicago Manual of Style (17th edition), AP Stylebook, and MLA Handbook — recommends a single space after a period. The double-space convention originated with monospaced typewriter fonts where extra spacing improved readability. Modern proportional fonts (used by all word processors, websites, and publications) render single spacing correctly. Using two spaces is not technically wrong, but it is considered outdated and is often flagged by editors and style checkers.

Why do extra spaces appear when I copy from a PDF?

PDF text extraction (copy-paste or OCR) frequently introduces irregular spacing for several reasons. PDFs store text as positioned characters, not flowing text — the extraction software must reconstruct word boundaries from character positions, often inserting spaces where none should be. Justified text has variable inter-word spacing that extracts as multiple space characters. Column layouts produce large gaps that extract as wide space runs. Hyphenated words at line ends may extract with spaces instead of rejoined. Running the extracted text through WritePadPro's Remove Extra Spaces tool collapses the extra spaces; words split apart by a stray space still need a manual check.

What is the difference between a regular space and a non-breaking space?

A regular space (Unicode U+0020) allows the browser or word processor to break the line at that point if needed for word wrapping. A non-breaking space (Unicode U+00A0, HTML  ) prevents line breaking — the words on either side always stay together on the same line. Non-breaking spaces are correct between numbers and units (100 km), titles and names (Mr. Smith), and other pairs that should not be separated. However, they often appear unintentionally in text copied from web pages or Word documents, causing some spacing cleanup tools to miss them because they are technically different characters from regular spaces. WritePadPro's Remove Extra Spaces tool converts them to regular spaces by default.

Does removing extra spaces affect my word count?

No. Word count is determined by splitting text at whitespace boundaries and counting the resulting tokens. Whether two words are separated by one space or five spaces, the word count remains the same — the same two tokens are produced. Removing extra spaces changes your character count (fewer characters) but not your word count. However, removing extra spaces can change your page count (slightly fewer pages due to tighter spacing) and character-based platform limits (you gain characters back when double spaces become single spaces).

Try These Tools

Related Articles