来自Perl,我肯定错过了"here-document"在源代码中创建多行字符串的方法:
$string = <<"EOF" # create a three-line string text text text EOF
在Java中,我必须在每一行都有繁琐的引号和加号,因为我从头开始连接多行字符串.
有哪些更好的选择?在属性文件中定义我的字符串?
编辑:两个答案说StringBuilder.append()优于加号表示法.任何人都可以详细说明他们为什么这么认为?它看起来并不比我更好.我正在寻找一种方法,即多行字符串不是一流的语言结构,这意味着我绝对不希望用方法调用替换第一类语言结构(字符串连接加上).
编辑:为了进一步澄清我的问题,我根本不关心表现.我担心可维护性和设计问题.
听起来你想要做一个多线文字,这在Java中是不存在的.
你最好的选择就是成为一起的弦乐+
.人们提到的其他一些选项(StringBuilder,String.format,String.join)只有在你开始使用字符串数组时才会更好.
考虑一下:
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";
对比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();
对比String.format()
:
String s = String.format("%s\n%s\n%s\n%s\n%s\n%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" );
与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" );
如果您想为您的特定系统的换行,你要么需要使用System.lineSeparator()
,也可以使用%n
在String.format
.
另一种选择是将资源放在文本文件中,只读取该文件的内容.这对于非常大的字符串来说是首选,以避免不必要地膨胀您的类文件.
在Eclipse中,如果打开选项"粘贴到字符串文字时转义文本"(在"首选项">"Java">"编辑器">"键入"中)并在引号中粘贴多行字符串,它将自动添加"
并\n" +
为您的所有行添加.
String str = "paste your text here";
Stephen Colebourne创建了一个在Java 7中添加多行字符串的提议.
此外,Groovy已经支持多行字符串.
这是一个旧的线程,但是一个新的非常优雅的解决方案(只有4个可能有3个小缺点)是使用自定义注释.
检查:http://www.adrianwalker.org/2011/12/java-multiline-string.html
受这项工作启发的项目在GitHub上托管:
https://github.com/benelog/multiline
Java代码示例:
import org.adrianwalker.multilinestring.Multiline; ... public final class MultilineStringUsage { /**
Hello
Multiline
World
缺点是
您必须激活相应的(提供的)注释处理器.
String变量不能定义为局部变量Check Raw String Literals项目,您可以在其中将变量定义为局部变量
String不能包含其他变量,如Visual Basic .Net with XML literal(<%= variable %>
):-)
字符串文字由JavaDoc注释(/**)分隔
您可能必须将Eclipse/Intellij-Idea配置为不自动重新格式化您的Javadoc注释.
有人可能会觉得这很奇怪(Javadoc的注释并不是为了嵌入除注释之外的任何东西),但由于Java中缺少多行字符串最终会让人烦恼,我觉得这是最差的解决方案.
另一个选项可能是将长字符串存储在外部文件中并将该文件读入字符串.
这是东西,你应该从来没有考虑它在做什么使用.但是对于一次性脚本我已经使用了这个非常成功:
例:
System.out.println(S(/* This is a CRAZY " ' ' " multiline string with all sorts of strange characters! */));
码:
// From: http://blog.efftinge.de/2008/10/multi-line-string-literals-in-java.html // Takes a comment (/**/) and turns everything inside the comment to a string that is returned from S() public static String S() { StackTraceElement element = new RuntimeException().getStackTrace()[1]; String name = element.getClassName().replace('.', '/') + ".java"; StringBuilder sb = new StringBuilder(); String line = null; InputStream in = classLoader.getResourceAsStream(name); String s = convertStreamToString(in, element.getLineNumber()); return s.substring(s.indexOf("/*")+2, s.indexOf("*/")); } // From http://www.kodejava.org/examples/266.html private static String convertStreamToString(InputStream is, int lineNum) { /* * To convert the InputStream to String we use the BufferedReader.readLine() * method. We iterate until the BufferedReader return null which means * there's no more data to read. Each line will appended to a StringBuilder * and returned as String. */ BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; int i = 1; try { while ((line = reader.readLine()) != null) { if (i++ >= lineNum) { sb.append(line + "\n"); } } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }
String.join
Java 8添加了一个新的静态方法,java.lang.String
它提供了一个更好的替代方案:
String.join(
CharSequence delimiter ,
CharSequence... elements
)
使用它:
String s = String.join( System.getProperty("line.separator"), "First line.", "Second line.", "The rest.", "And the last!" );
如果在属性文件中定义字符串,它看起来会更糟糕.IIRC,它看起来像:
string:text\u000atext\u000atext\u000a
通常,不将大字符串嵌入到源代码中是一个合理的想法.您可能希望将它们作为资源加载,可能是XML或可读文本格式.文本文件可以在运行时读取,也可以编译为Java源代码.如果你最终把它们放在源代码中,我建议把它+
放在前面并省略不必要的新行:
final String text = "" +"text " +"text " +"text" ;
如果您有新行,您可能需要一些连接或格式化方法:
final String text = join("\r\n" ,"text" ,"text" ,"text" );
将两个字符串转换为StringBuilder.append,除非两个字符串都是常量,因此编译器可以在编译时将它们组合在一起.至少,这就是Sun编译器中的情况,如果不是所有其他编译器都会这样做,我会怀疑大多数编译器.
所以:
String a="Hello"; String b="Goodbye"; String c=a+b;
通常生成完全相同的代码:
String a="Hello"; String b="Goodbye": StringBuilder temp=new StringBuilder(); temp.append(a).append(b); String c=temp.toString();
另一方面:
String c="Hello"+"Goodbye";
是相同的:
String c="HelloGoodbye";
也就是说,在多行中打破你的字符串文字没有任何惩罚,加号为可读性.
JEP 326:Raw String Literals将实现多行字符串,因此您可以编写如下内容:
String s = """ text text text """;
在IntelliJ IDE中,您只需键入:
""
然后将光标放在引号内并粘贴您的字符串.IDE将它扩展为多个连接行.
遗憾的是,Java没有多行字符串文字.您必须连接字符串文字(使用+或StringBuilder是两种最常用的方法)或从单独的文件中读取字符串.
对于大型多行字符串文字,我倾向于使用单独的文件并使用getResourceAsStream()
(Class
类的方法)读取它.这使您可以轻松找到该文件,因为您不必担心当前目录与安装代码的位置.它还使包装更容易,因为您实际上可以将文件存储在jar文件中.
假设你在一个名为Foo的类中.做这样的事情:
Reader r = new InputStreamReader(Foo.class.getResourceAsStream("filename"), "UTF-8"); String s = Utils.readAll(r);
另一个烦恼是Java没有标准"将此Reader中的所有文本读入字符串"方法.这写起来很容易:
public static String readAll(Reader input) { StringBuilder sb = new StringBuilder(); char[] buffer = new char[4096]; int charsRead; while ((charsRead = input.read(buffer)) >= 0) { sb.append(buffer, 0, charsRead); } input.close(); return sb.toString(); }
String newline = System.getProperty ("line.separator"); string1 + newline + string2 + newline + string3
但是,最好的选择是使用String.format
String multilineString = String.format("%s\n%s\n%s\n",line1,line2,line3);
您可以使用与java兼容的scala-code,并允许使用"""括起来的多行字符串:
package foobar
object SWrap {
def bar = """John said: "This is
a test
a bloody test,
my dear." and closed the door."""
}
(注意字符串中的引号)和java:
String s2 = foobar.SWrap.bar ();
这是否更舒服......?
另一种方法,如果你经常处理长文本,它应放在你的源代码中,可能是一个脚本,它从外部文件获取文本,并将其作为multiline-java-String包装,如下所示:
sed '1s/^/String s = \"/;2,$s/^/\t+ "/;2,$s/$/"/' file > file.java
这样您就可以轻松地将其剪切并粘贴到源中.
由于Java本身不支持多行字符串,因此现在唯一的方法是使用上述技术之一来破解它.我使用上面提到的一些技巧构建了以下Python脚本:
import sys import string import os print 'new String(' for line in sys.stdin: one = string.replace(line, '"', '\\"').rstrip(os.linesep) print ' + "' + one + ' "' print ')'
将它放在名为javastringify.py的文件中,并将您的字符串放在mystring.txt文件中,并按如下方式运行:
cat mystring.txt | python javastringify.py
然后,您可以复制输出并将其粘贴到编辑器中.
根据需要修改它以处理任何特殊情况,但这适用于我的需要.希望这可以帮助!
您可以使用单独的方法连接附加内容,例如:
public static String multilineString(String... lines){ StringBuilder sb = new StringBuilder(); for(String s : lines){ sb.append(s); sb.append ('\n'); } return sb.toString(); }
无论哪种方式,更喜欢StringBuilder
加号表示法.
请参阅Java Stringfier.如果需要,将文本转换为StringBuilder java块转义.
import org.apache.commons.lang3.StringUtils; String multiline = StringUtils.join(new String[] { "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", }, "\n");
实际上,以下是迄今为止我见过的最干净的实现.它使用注释将注释转换为字符串变量...
/**
Hello
Multiline
World
因此,最终结果是变量html包含多行字符串.没有引号,没有加号,没有逗号,只是纯粹的字符串.
此解决方案可从以下URL获得... http://www.adrianwalker.org/2011/12/java-multiline-string.html
希望有所帮助!
我没有看到答案的替代方案是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();
还有java.io.BufferedWriter
一个newLine()
方法没有提及的事实.
如果你像我一样喜欢谷歌的番石榴,它可以提供一个相当干净的表示和一个很好的,简单的方法来不硬编码您的换行符:
String out = Joiner.on(newline).join(ImmutableList.of( "line1", "line2", "line3"));
使用Properties.loadFromXML(InputStream)
。不需要外部库。
比凌乱的代码更好(因为您需要关注可维护性和设计),因此最好不要使用长字符串。
首先阅读xml属性:
InputStream fileIS = YourClass.class.getResourceAsStream("MultiLine.xml"); Properties prop = new Properies(); prop.loadFromXML(fileIS);
那么您可以以更易于维护的方式使用多行字符串...
static final String UNIQUE_MEANINGFUL_KEY = "Super Duper UNIQUE Key"; prop.getProperty(UNIQUE_MEANINGFUL_KEY) // "\n MEGA\n LONG\n..."
MultiLine.xml`位于相同的文件夹YourClass中:
MEGA LONG MULTILINE
PS .:您可以将...
"]]>
用于类似xml的字符串。