Java multiline string

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

    If you define your strings in a properties file it'll look much worse. IIRC, it'll look like:

    string:text\u000atext\u000atext\u000a
    

    Generally it's a reasonable idea to not embed large strings in to source. You might want to load them as resources, perhaps in XML or a readable text format. The text files can be either read at runtime or compiled into Java source. If you end up placing them in the source, I suggest putting the + at the front and omitting unnecessary new lines:

    final String text = ""
        +"text "
        +"text "
        +"text"
    ;
    

    If you do have new lines, you might want some of join or formatting method:

    final String text = join("\r\n"
        ,"text"
        ,"text"
        ,"text"
    );
    
    0 讨论(0)
  • 2020-11-22 16:21
    String newline = System.getProperty ("line.separator");
    string1 + newline + string2 + newline + string3
    

    But, the best alternative is to use String.format

    String multilineString = String.format("%s\n%s\n%s\n",line1,line2,line3);
    
    0 讨论(0)
  • 2020-11-22 16:22

    Since Java does not (yet) native support multi-line strings, the only way for now is to hack around it using one of the aforementioned techniques. I built the following Python script using some of the tricks mentioned above:

    import sys
    import string
    import os
    
    print 'new String('
    for line in sys.stdin:
        one = string.replace(line, '"', '\\"').rstrip(os.linesep)
        print '  + "' + one + ' "'
    print ')'
    

    Put that in a file named javastringify.py and your string in a file mystring.txt and run it as follows:

    cat mystring.txt | python javastringify.py
    

    You can then copy the output and paste it into your editor.

    Modify this as needed to handle any special cases but this works for my needs. Hope this helps!

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 16:24

    If you like google's guava as much as I do, it can give a fairly clean representation and a nice, easy way to not hardcode your newline characters too:

    String out = Joiner.on(newline).join(ImmutableList.of(
        "line1",
        "line2",
        "line3"));
    
    0 讨论(0)
  • 2020-11-22 16:24

    I know this is an old question, however for intersted developers Multi line literals gonna be in #Java12

    http://mail.openjdk.java.net/pipermail/amber-dev/2018-July/003254.html

    0 讨论(0)
提交回复
热议问题