Java multiline string

前端 未结 30 2921
醉梦人生
醉梦人生 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条回答
  •  萌比男神i
    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!

提交回复
热议问题