Java multiline string

前端 未结 30 2953
醉梦人生
醉梦人生 2020-11-22 15:55

Coming from Perl, I sure am missing the \"here-document\" means of creating a multi-line string in source code:

$string = <<\"EOF\"  # create a three-l         


        
30条回答
  •  眼角桃花
    2020-11-22 16:19

    String.join

    Java 8 added a new static method to java.lang.String which offers a slightly better alternative:

    String.join( CharSequence delimiter , CharSequence... elements )

    Using it:

    String s = String.join(
        System.getProperty("line.separator"),
        "First line.",
        "Second line.",
        "The rest.",
        "And the last!"
    );
    

提交回复
热议问题