Collapse Whitespace Tool
Paste text to collapse runs of whitespace into single spaces.
Result
How it is calculated
The tool replaces every run of one or more whitespace characters with a single space, using the pattern \s+ in Unicode mode, and then (by default) trims the space left at the start and end. Whitespace here means ordinary spaces, tabs, line breaks and also non-breaking or ideographic spaces, which become a normal space. Lengths are counted in characters, not bytes, so accented letters count as one. The removed figure is original length - new length. With trimming switched off, a run at the start or end collapses to one space instead of disappearing. An empty input gives no result. Text made only of whitespace returns an empty string when trimming is on.
Worked example
Input: two spaces, Hello, three spaces, world,, two line breaks, a tab, this, two spaces, is, three spaces, a, two spaces, test., two trailing spaces. The tool reports 40 chars. Each run becomes one space, and the leading and trailing runs are trimmed. Result: Hello world, this is a test. The new length is 28 chars, so 12 chars were removed. With trim off, the result keeps one space at each end: 30 chars, 10 chars removed.
Common mistakes
- Expecting line breaks to survive. Paragraph breaks are also whitespace, so multi-line text becomes one single line.
- Using it to clean code or indented lists. Indentation is collapsed too, so it is not suitable for Python, YAML or tables aligned with spaces.
- Turning trim off and then wondering about a leftover space at the start or end. Each edge run becomes one space.
- Expecting zero-width characters to disappear. Only characters treated as whitespace are collapsed; hidden zero-width marks are not guaranteed to be removed.
- Comparing the removed count to the number of spaces you see. It counts all characters dropped, including tabs and line breaks.
About this calculator
This tool applies a simple but common text-normalization pattern: it matches every run of one or more whitespace characters — spaces, tabs, and line breaks alike, typically via a regular expression like /\s+/ — and replaces each run with a single space, then trims any leading or trailing space from the result.
Text pasted from PDFs, emails, or scraped web pages often carries stray tabs, double spaces, and inconsistent line breaks that break string comparisons, CSV parsing, or search matching. Developers cleaning input data, data-entry and QA teams, and copywriters preparing text for publishing use whitespace collapsing to normalize messy text into a single clean line before it's stored, compared, or displayed.
Was this helpful?
You can pick up to 3 reactions.
Popular calculators
Loan Calculator
Monthly payment, total interest and full amortization schedule.
Percentage Calculator
Percent change, percent of, and ratio calculations.
Discount Calculator
Calculate final price, savings, and discount amount.
BMI Calculator
Body mass index from height and weight (metric or imperial).
Age Calculator
Compute your exact age in years, months and days from a birth date.
Compound Interest Calculator
Calculate the final amount and interest earned with compounding.
All Calculators
- Loan Calculator
- Profit Calculator
- Discount Calculator
- BMI Calculator
- Percentage Calculator
- Ideal Weight Calculator
- Age Calculator
- Pregnancy Due Date Calculator
- Compound Interest Calculator
- BMR Calculator (Basal Metabolic Rate)
- Tip Calculator
- Fuel Cost Calculator
- Date Difference Calculator
- Sleep Cycle Calculator
- Daily Water Intake Calculator
- Simple Interest Calculator
- Heart Rate Zones Calculator
- Running Pace Calculator
- Pet Age Calculator
- Calories Burned Calculator
- Macro Calculator
- TDEE Calculator (Total Daily Energy Expenditure)
- Waist-to-Hip Ratio Calculator
- Honor Roll Calculator
- One Rep Max (1RM) Calculator
- Ovulation Calculator
- Electricity Cost Calculator
- Unit Price Calculator
- Savings Goal Calculator
- Break-Even Point Calculator
- Paint Calculator
- Course Grade Average Calculator
- Business Days Calculator
- Time Duration Calculator
- Fuel Economy Calculator
- Temperature Converter
- Tile Calculator
- Aspect Ratio Calculator
- Roman Numeral Converter
- Goal Weight Timeline Calculator
- Salary Converter (Hourly to Annual)
- Credit Card Validator (Luhn)
- Body Surface Area (BSA) Calculator
- Feels-Like Temperature Calculator
- Ohm’s Law Calculator
- PPI (Pixel Density) Calculator
- Molar Mass Calculator
- Zodiac Sign Calculator (Western + Chinese)
- IBAN Validator
- Shoe Size Converter
- Country Info & Dialing Code Lookup
- Time Zone Difference (World Clock)
- CAGR (Compound Annual Growth Rate) Calculator
- Rule of 72 Calculator
- Blood Alcohol Content (BAC) Calculator
- Lean Body Mass (LBM) Calculator
- Pythagorean Theorem Calculator
- Speed Converter
- Download Time Calculator
- Date Calculator (Add/Subtract)
No calculators found
Comments (1)
Text copied from a PDF often breaks words with a hyphen at the end of a line. This tool joins the lines with a single space but leaves the hyphen alone, so you may still see something like exam- ple in the output. Collapse first, then search for a hyphen followed by a space and fix those by hand. Which source do you most often clean up: PDFs, emails or scraped pages?