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。文字列の改行コードをCRLFからLFに変換

Posted at

null の場合は例外を発生させず、空文字 "" を返す。
replace("\r\n", "\n") を使用して CRLF を LF に変換。

public class LineSeparatorConverter {
    public static String convertCRLFtoLF(String input) {
        if (input == null) {
            return ""; // nullを空文字列に変換
        }
        return input.replace("\r\n", "\n");
    }

    public static void main(String[] args) {
        String test1 = "Hello\r\nWorld\r\n!";
        String test2 = null;

        System.out.println("Converted String 1:");
        System.out.println(convertCRLFtoLF(test1));

        System.out.println("Converted String 2:");
        System.out.println("[" + convertCRLFtoLF(test2) + "]"); // 空文字で囲んで表示
    }
}
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?