0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java学習記録 #テキストブロック

Posted at

テキストブロックとは

Java SE 15で正式導入された新しい文字列の表現方法です。
複数行にわたる文字列を、シンプルかつ読みやすい形式で書けるようになりました。

使い方

テキストブロックは、"""(ダブルクォート3つ)で囲むことで定義できます。

String text = """
    Hello,
    Java World!
    """;
System.out.println(text);

🔹 文字列内の 改行もそのまま反映 されます。
🔹 末尾の「"""」 は新しい行から始めるのがルールです。

テキストブロック導入で変わったこと

従来の問題点 テキストブロック導入後の改善
長い文字列や複数行文字列は + で連結しないと書けなかった 複数行をそのまま自然な形で記述できる
\n\" のエスケープが多くて読みにくい 自然な文章のままで記述可能
JSONやHTMLなどを書くとコードが崩れる フォーマット保持したまま書けて視認性向上
改行・インデントの取り扱いが面倒 自動インデント調整、改行も含まれる
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?