Remove Line Breaks

Replace line breaks with a space or with nothing to join lines.

Back to all tools on ToolForge

More in Text Tools

Input



Result

About Remove Line Breaks

This tool removes line breaks from text by replacing newline characters with a space or custom separator. It handles all three line ending types: LF (Unix/Linux), CRLF (Windows), and CR (legacy Mac). The tool uses JavaScript's string split and join operations to process text.

It is useful for cleaning up pasted text from PDFs or emails, preparing CSV data for import, joining wrapped log lines, formatting code snippets, removing artificial line breaks in JSON or SQL queries, and consolidating multi-line paragraphs into single lines for processing.

Line Break Types

Line breaks are control characters that mark the end of a text line:

Line Ending Types:

1. LF (Line Feed)
   - Character: \n
   - ASCII: 10 (0x0A)
   - Used by: Unix, Linux, macOS (OS X+), iOS, Android
   - JavaScript: "line1\nline2"

2. CRLF (Carriage Return + Line Feed)
   - Characters: \r\n
   - ASCII: 13 + 10 (0x0D 0x0A)
   - Used by: Windows, DOS, Symbian OS
   - JavaScript: "line1\r\nline2"

3. CR (Carriage Return)
   - Character: \r
   - ASCII: 13 (0x0D)
   - Used by: Legacy Mac OS (pre-OS X), older systems
   - JavaScript: "line1\rline2"

Historical Note:
  CR and LF come from typewriters:
  - Carriage Return: return carriage to start of line
  - Line Feed: advance paper to next line
  Windows kept both; Unix simplified to LF only.

Operating System Line Endings

Operating System Line Ending Characters Hex
Windows 10/11 CRLF \r\n 0D 0A
Linux (all distros) LF \n 0A
macOS (OS X+) LF \n 0A
Classic Mac OS (pre-X) CR \r 0D
Android LF \n 0A
iOS LF \n 0A

Line Break Removal Methods

Method 1: Join with Space
  Input:
    "Hello
    World"

  Output: "Hello World"

  Use case: Joining sentences, paragraphs

Method 2: Join without Space
  Input:
    "Hello
    World"

  Output: "HelloWorld"

  Use case: Concatenating code, URLs, identifiers

Method 3: Normalize Line Endings (CRLF → LF)
  Input: "line1\r\nline2\r\nline3"
  Output: "line1\nline2\nline3"

  Use case: Cross-platform file conversion

Method 4: Replace with Custom Separator
  Input:
    "apple
    banana
    cherry"

  Output (comma): "apple,banana,cherry"
  Output (pipe): "apple|banana|cherry"

  Use case: CSV generation, list formatting

Common Use Cases

Use Case Description Separator
PDF Text Cleanup Remove artificial breaks from PDF copy/paste Space
Email Formatting Join wrapped email lines for clean quoting Space
CSV Preparation Convert multi-line cells to single line Space or custom
Code Formatting Join wrapped code lines (e.g., long strings) None
Log File Processing Join wrapped log entries for analysis Space or marker
JSON/SQL Cleanup Remove breaks from strings in queries None or escaped
List to CSV Convert vertical list to comma-separated Comma

Code Examples by Language

JavaScript:
  // Remove all line breaks, join with space
  text.split(/\r?\n/).join(' ')

  // Remove all line breaks, no separator
  text.split(/\r?\n/).join('')

  // Normalize CRLF to LF
  text.replace(/\r\n/g, '\n').replace(/\r/g, '\n')

  // Replace with custom separator
  text.split(/\r?\n/).join(',')

Python:
  # Remove all line breaks, join with space
  ' '.join(text.splitlines())

  # Normalize line endings
  text.replace('\r\n', '\n').replace('\r', '\n')

  # Replace with custom separator
  ','.join(text.splitlines())

Bash/sed:
  # Join all lines with space
  tr '\n' ' ' < file.txt

  # Remove all newlines
  tr -d '\n' < file.txt

  # CRLF to LF conversion
  sed 's/\r$//' file.txt

PowerShell:
  # Join with space
  (Get-Content file.txt) -join ' '

  # Normalize line endings
  (Get-Content file.txt) -join "`n"

PHP:
  // Join with space
  implode(' ', file('file.txt'));

  // Normalize line endings
  str_replace(["\r\n", "\r"], "\n", $text);

Regex Patterns for Line Breaks

Pattern       | Matches                  | Notes
--------------|--------------------------|------------------
\n            | LF only                  | Unix/Linux/macOS
\r\n          | CRLF                     | Windows
\r            | CR only                  | Legacy Mac
\r?\n         | LF or CRLF               | Cross-platform
[\r\n]+       | Any line break(s)        | One or more
(?:\r\n|\r|\n)| Any line break (grouped) | Explicit alternation

JavaScript Examples:
  // Split on any line break type
  text.split(/\r?\n/)

  // Replace all line breaks with space
  text.replace(/[\r\n]+/g, ' ')

  // Normalize to LF
  text.replace(/\r\n/g, '\n').replace(/\r/g, '\n')

Line Break Removal Examples

Example 1: Simple Paragraph
  Input:
    "Hello
    World"

  Join with space: "Hello World"
  Join (no space): "HelloWorld"

Example 2: Multiple Lines
  Input:
    "Line 1
    Line 2
    Line 3"

  Join with space: "Line 1 Line 2 Line 3"
  Join (no space): "Line1Line2Line3"

Example 3: CRLF (Windows) Line Endings
  Input: "Line1\r\nLine2\r\nLine3"

  After removal: "Line1 Line2 Line3"

Example 4: Mixed Line Endings
  Input: "Line1\nLine2\r\nLine3\rLine4"

  Normalized: "Line1\nLine2\nLine3\nLine4"

Example 5: CSV Data
  Input:
    "name,age
    John,30
    Jane,25"

  Join with comma: "name,age,John,30,Jane,25"
  Join with semicolon: "name,age;John,30;Jane,25"

Example 6: Code String
  Input:
    "const msg = 'Hello
    World';"

  Join (no space): "const msg = 'HelloWorld';"
  Join with space: "const msg = 'Hello World';"

Example 7: Wrapped Log Entry
  Input:
    "2024-01-15 10:30:45 ERROR: Database
    connection failed after 30000ms
    timeout"

  Join with space:
    "2024-01-15 10:30:45 ERROR: Database connection failed after 30000ms timeout"

Example 8: HTML List to Inline
  Input:
    "
  • Item 1
  • Item 2
  • Item 3
  • " Join (no space): "
  • Item 1
  • Item 2
  • Item 3
  • "

    Line Ending Conversion

    Conversion Matrix:
    
    From → To    | LF (\n) | CRLF (\r\n) | CR (\r)
    -------------|---------|-------------|--------
    LF (\n)      | -       | \n → \r\n   | \n → \r
    CRLF (\r\n)  | \r\n → \n | -         | \r\n → \r
    CR (\r)      | \r → \n | \r → \r\n   | -
    
    Common Conversions:
    
    1. Windows to Unix (CRLF → LF):
       text.replace(/\r\n/g, '\n')
    
    2. Unix to Windows (LF → CRLF):
       text.replace(/\n/g, '\r\n')
    
    3. Legacy Mac to Unix (CR → LF):
       text.replace(/\r/g, '\n')
    
    4. Normalize all to LF:
       text.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
    
    5. Remove all line breaks:
       text.replace(/[\r\n]+/g, '')
    

    Best Practices

    Limitations

    Frequently Asked Questions

    What are line breaks and how do they work?
    Line breaks are special characters that mark the end of a line of text. The three main types are LF (Line Feed, \n, Unix/Linux/macOS), CRLF (Carriage Return + Line Feed, \r\n, Windows), and CR (Carriage Return, \r, legacy Mac). When text is split into lines, these characters separate each line.
    What is the difference between LF, CRLF, and CR?
    LF (\n) is a single character used by Unix, Linux, and modern macOS. CRLF (\r\n) is two characters used by Windows. CR (\r) is a single character used by legacy Mac OS (pre-OS X). CRLF originates from typewriters requiring two actions: carriage return and line feed.
    When should I join lines with space vs without space?
    Join with space when lines contain sentences or paragraphs that should flow naturally. Join without space when lines contain code, URLs, or data that should concatenate directly. For example, joining 'Hello\nWorld' with space gives 'Hello World'; without space gives 'HelloWorld'.
    How do I normalize line endings?
    To normalize line endings, first convert all CRLF to LF by replacing \r\n with \n, then convert any remaining CR to LF by replacing \r with \n. This ensures consistent LF line endings across all platforms. Many code editors offer this as a built-in feature.
    What are common use cases for removing line breaks?
    Common uses include: cleaning up pasted text from PDFs or emails, preparing CSV data for import, joining wrapped log lines, formatting code snippets, removing artificial line breaks in JSON or SQL queries, and consolidating multi-line paragraphs into single lines for processing.
    Can I use a custom separator instead of space or nothing?
    Yes, line breaks can be replaced with any custom separator such as comma, semicolon, pipe character, or HTML break tags. This is useful for creating CSV output (comma), SQL lists (comma), or preparing text for HTML display (br tags).