この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:7
ページ更新者:T
更新日時:2026-06-11 07:07:02

タイトル: 改行コードの削除
SEOタイトル: 【Java】改行コードの削除

この記事の要点
  • 改行コードの削除方法(言語別)
  • Java: str.replaceAll("\r?\n", "") または str.replace("\n", "")
  • PHP: str_replace([\"\r\", \"\n\"], \"\", $str)
  • Python: str.replace("\n", "").replace("\r", "") / splitlines()
  • JavaScript: str.replace(/\r?\n/g, "")
  • 改行コードは OS で違う: Windows \r\n / Unix \n / Mac (旧) \r

 

改行コードの種類

OS改行コード表記
WindowsCR + LF\\r\\n (0x0D 0x0A)
Unix / Linux / macOS (現代)LF\\n (0x0A)
macOS (9 以前)CR\\r (0x0D)

Java

String text = "line1\nline2\r\nline3\rline4";

// 全種類の改行を削除
String result = text.replaceAll("\\r?\\n|\\r", "");
// → "line1line2line3line4"

// 改行を半角スペースに置換
String result = text.replaceAll("\\r?\\n", " ");

// 末尾の改行のみ削除 (trim)
String result = text.replaceAll("\\s+$", "");

// 連続改行を 1 つに正規化
String result = text.replaceAll("(\\r?\\n)+", "\n");

// 行ごとに処理 → 結合
String result = String.join("", text.split("\\r?\\n"));

// Java 11+ String.lines() を使う
String result = text.lines().collect(Collectors.joining());

PHP

Python

text = "line1\nline2\r\nline3\rline4"

# 全種類の改行を削除
result = text.replace("\r\n", "").replace("\n", "").replace("\r", "")

# 正規表現で一括
import re
result = re.sub(r"\r?\n|\r", "", text)

# 行に分割 → 結合
result = "".join(text.splitlines())

# 改行をスペースに
result = re.sub(r"\r?\n", " ", text)

# 末尾の改行のみ削除
result = text.rstrip()
result = text.rstrip("\r\n")

# 前後の空白・改行を全削除
result = text.strip()

# 連続改行の正規化
result = re.sub(r"(\r?\n)+", "\n", text)

# ファイル読み込み時の改行除去
with open("file.txt") as f:
    lines = [line.rstrip() for line in f]

JavaScript

const text = "line1\nline2\r\nline3\rline4";

// 全種類の改行を削除
const result = text.replace(/\r?\n|\r/g, "");

// 改行をスペースに
const result = text.replace(/\r?\n/g, " ");

// 末尾の改行のみ削除
const result = text.replace(/\s+$/, "");
const result = text.trimEnd();  // ES2019

// 前後の空白・改行を全削除
const result = text.trim();

// 連続改行の正規化
const result = text.replace(/(\r?\n)+/g, "\n");

// 行ごとに処理
const lines = text.split(/\r?\n/);
const result = lines.join("");

シェル / コマンドライン

sed

# 全改行削除(tr が確実)
$ echo -e "line1\nline2" | tr -d "\n"
line1line2

# CR (\r) のみ削除 (CRLF → LF 変換)
$ sed -i "s/\r//g" file.txt

# Windows 改行 → Unix 改行
$ dos2unix file.txt

# 逆
$ unix2dos file.txt

# 連続改行を 1 つに
$ sed -i "/^$/d" file.txt  # 空行削除
$ awk "NF" file.txt        # 同上

awk

# 各行末の \r を削除
$ awk "{sub(/\r$/, \"\")} 1" file.txt

# 全改行を削除(1 行に結合)
$ awk "{printf \"%s\", \$0}" file.txt

SQL での改行削除

MySQL

-- 改行をスペースに置換
UPDATE users
SET description = REPLACE(REPLACE(description, CHAR(13), ' '), CHAR(10), ' ')
WHERE id = 1;

-- 改行を削除
UPDATE users
SET description = REPLACE(REPLACE(description, CHAR(13), ''), CHAR(10), '');

-- 検索(改行を含むレコード)
SELECT * FROM users WHERE description LIKE CONCAT('%', CHAR(10), '%');

PostgreSQL

-- 改行削除
UPDATE users
SET description = REPLACE(REPLACE(description, E'\n', ''), E'\r', '');

-- 正規表現
UPDATE users
SET description = REGEXP_REPLACE(description, E'\r?\n|\r', '', 'g');

Oracle

-- 改行削除
UPDATE users
SET description = REPLACE(REPLACE(description, CHR(13), ''), CHR(10), '');

用途別の使い分け

用途手法
CSV エクスポートで改行を半角空白に正規表現で \\r?\\n → スペース
ファイル末尾の改行のみ削除rstrip() / trimEnd()
連続改行を 1 つに正規化正規表現 (\\r?\\n)+
Windows ⇄ Unix 変換dos2unix / unix2dos
HTML 表示用nl2br()(PHP)等で

注意点

  • OS による違い: ファイル受け渡し時は Windows / Mac / Unix の改行を全部考慮
  • UTF-8 BOM: ファイル先頭の \\xEF\\xBB\\xBF も別途削除が必要なことがある
  • 正規表現の挙動: 各言語で \\r / \\n の扱いが微妙に違う
  • パフォーマンス: 大量データなら正規表現より str_replace 系の方が速い

関連記事