Java multiline string
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 line string
text
text
text
EOF
In Java I have to have cumbersome quotes and plus signs on every line as I concatenate my multiline string from scratch.
What are some better alternatives? Define my string in a properties file?
Edit : Two answers say StringBuilder.append() is preferable to the plus notation. Could anyone elaborate as to why they think so? It doesn't look more preferable to me at all. I'm looking for away around the fact that multiline strings are not a first-class language construct, which means I definitely don't want to replace a first-class language construct (string concatenation with plus) with method calls.
Edit : To clarify my question further, I'm not concerned about performance at all. I'm concerned about maintainability and design issues.
Stephen Colebourne has created a proposal for adding multi-line strings in Java 7.
Also, Groovy already has support for multi-line strings.
It sounds like you want to do a multiline literal, which does not exist in Java.
Your best alternative is going to be strings that are just +
'd together. Some other options people have mentioned (StringBuilder, String.format, String.join) would only be preferable if you started with an array of strings.
Consider this:
String s = "It was the best of times, it was the worst of times,n"
+ "it was the age of wisdom, it was the age of foolishness,n"
+ "it was the epoch of belief, it was the epoch of incredulity,n"
+ "it was the season of Light, it was the season of Darkness,n"
+ "it was the spring of hope, it was the winter of despair,n"
+ "we had everything before us, we had nothing before us";
Versus StringBuilder
:
String s = new StringBuilder()
.append("It was the best of times, it was the worst of times,n")
.append("it was the age of wisdom, it was the age of foolishness,n")
.append("it was the epoch of belief, it was the epoch of incredulity,n")
.append("it was the season of Light, it was the season of Darkness,n")
.append("it was the spring of hope, it was the winter of despair,n")
.append("we had everything before us, we had nothing before us")
.toString();
Versus String.format()
:
String s = String.format("%sn%sn%sn%sn%sn%s"
, "It was the best of times, it was the worst of times,"
, "it was the age of wisdom, it was the age of foolishness,"
, "it was the epoch of belief, it was the epoch of incredulity,"
, "it was the season of Light, it was the season of Darkness,"
, "it was the spring of hope, it was the winter of despair,"
, "we had everything before us, we had nothing before us"
);
Versus Java8 String.join()
:
String s = String.join("n"
, "It was the best of times, it was the worst of times,"
, "it was the age of wisdom, it was the age of foolishness,"
, "it was the epoch of belief, it was the epoch of incredulity,"
, "it was the season of Light, it was the season of Darkness,"
, "it was the spring of hope, it was the winter of despair,"
, "we had everything before us, we had nothing before us"
);
If you want the newline for your particular system, you either need to use System.getProperty("line.separator")
, or you can use %n
in String.format
.
Another option is to put the resource in a text file, and just read the contents of that file. This would be preferable for very large strings to avoid unnecessarily bloating your class files.
在Eclipse中,如果打开选项“粘贴到字符串文本时转义文本”(在首选项> Java>编辑器>键入中)并粘贴引号内的多行字符串,则会自动为所有字符添加"
和n" +
你的线条。
String str = "paste your text here";
链接地址: http://www.djcxy.com/p/20904.html
上一篇: 将字符串编号转换为整数
下一篇: Java多行字符串