Java multiline string

前端 未结 30 2906
醉梦人生
醉梦人生 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:23

    An alternative I haven't seen as answer yet is the java.io.PrintWriter.

    StringWriter stringWriter = new StringWriter();
    PrintWriter writer = new PrintWriter(stringWriter);
    writer.println("It was the best of times, it was the worst of times");
    writer.println("it was the age of wisdom, it was the age of foolishness,");
    writer.println("it was the epoch of belief, it was the epoch of incredulity,");
    writer.println("it was the season of Light, it was the season of Darkness,");
    writer.println("it was the spring of hope, it was the winter of despair,");
    writer.println("we had everything before us, we had nothing before us");
    String string = stringWriter.toString();
    

    Also the fact that java.io.BufferedWriter has a newLine() method is unmentioned.

提交回复
热议问题