来自Perl,我肯定错过了在源代码中创建多行字符串的“here-document”方法:

$string = <<"EOF"  # create a three-line string
text
text
text
EOF

在Java中,当我从头开始连接多行字符串时,我必须在每一行上使用繁琐的引号和加号。

有什么更好的选择吗?在属性文件中定义我的字符串?

编辑:有两个答案说StringBuilder.append()比加号更可取。谁能详细解释一下他们为什么这么想?在我看来,这一点也不可取。我正在寻找一种方法来解决多行字符串不是一级语言结构这一事实,这意味着我绝对不想用方法调用取代一级语言结构(字符串连接与加号)。

编辑:为了进一步澄清我的问题,我根本不关心性能。我关心的是可维护性和设计问题。


当前回答

Java 13预览:

Text Blocks Come to Java. Java 13 delivers long-awaited multiline string by Mala Gupta With text blocks, Java 13 is making it easier for you to work with multiline string literals. You no longer need to escape the special characters in string literals or use concatenation operators for values that span multiple lines. Text block is defined using three double quotes (""") as the opening and closing delimiters. The opening delimiter can be followed by zero or more white spaces and a line terminator.

例子:

 String s1 = """
 text
 text
 text
 """;

其他回答

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);

加号被转换为StringBuilder。除非两个字符串都是常量,以便编译器可以在编译时将它们组合在一起。至少,Sun的编译器是这样的,我怀疑大多数(如果不是所有)其他编译器也会这样做。

So:

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";

也就是说,为了可读性,在多行中使用加号将字符串字面量分开并不会受到惩罚。

这个问题有两个答案:

In you want to stick to pure Java, with Java 14 being released in March 2020, you can leverage the JEP 368 - Text Blocks, in Second Preview mode. Actually the feature is in preview mode in other releases (at least 13 has it). I created and example set here. While this feature is useful, it can be easily abused. Remember that Java requires compilation - having large character arrays in your code can be an easy way to shoot yourself in the leg (if you want a quick change, you will need recompilation - that toolset might not be available to the guy operating your application).

根据我的经验,建议在配置文件中保留大字符串(通常是应用程序操作员可以/应该在运行时更改的字符串)。

总结:负责地使用文本块:)。

我建议使用ThomasP所建议的实用程序,然后将其链接到构建过程中。仍然存在一个外部文件以包含文本,但在运行时不读取该文件。 工作流程如下:

构建一个“文本文件到java代码”实用程序并检查到版本控制 在每次构建时,针对资源文件运行实用程序以创建修改后的java源 Java源代码包含类似于 类TextBlock{…后面跟着一个从资源文件自动生成的静态字符串 用剩下的代码构建生成的java文件

使用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:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
    <entry key="Super Duper UNIQUE Key">
       MEGA
       LONG
       MULTILINE
    </entry>
</properties>

注:你可以使用<![CDATA[”…"]]>为类似xml的字符串。