StringBuilder每次都将内容添加到一条新行中。

时间:2022-10-16 13:57:56

I am building a validation routine that validates contents and then gives warning (for failures) in form of StringBuilder. Say in below code I am checking lower bound for values paramX and paramY.

我正在构建一个验证例程,该例程验证内容,然后以StringBuilder的形式给出警告(对于失败)。在下面的代码中,我正在检查参数paramX和paramY值的下界。

 StringBuilder sb= new StringBuilder();

        if(paramX<10){
            sb.append("paramX cannot be less than 10 ");
        }

        if(paramY<20){
            sb.append("paramY cannot be less than 20 ");
        }

        System.out.println(sb);

It gives output as: paramX cannot be less than 10 paramY cannot be less than 20

输出为:paramX不能小于10 paramY不能小于20

but i want output such that, each appended String will be printed on new line. Like below.

但是我想输出这样的输出,每个附加的字符串将被打印到新的行上。像下面。

paramX cannot be less than 10 

paramY cannot be less than 20

I used following workarounds, but ended up repeating same code again and again(Which i don't want to).

我使用了以下的变通方法,但最终重复了一遍又一遍的代码(我不想这样做)。

sb.append(System.getProperty("line.separator")); // Add Explicit line separator each time
sb.append("\n");
sb.append("paramX cannot be less than 10 \n");

Is there a simpler way to do it?

有更简单的方法吗?

7 个解决方案

#1


-3  

simply append directly...

直接添加…

        if(paramX<10){
            sb.append("paramX cannot be less than 10 \n ");
        }

        if(paramY<20){
            sb.append("paramY cannot be less than 20 \n ");
        }

#2


6  

If you don't want to do it over and over then write a helper method:

如果你不想一遍又一遍地做,那就写一个辅助方法:

public void appendString(StringBuilder builder, String value) {
    builder.append(value + System.getProperty("line.separator"));
}

Then call:

然后调用:

if(paramX<10){
    appendString(sb, "paramX cannot be less than 10 ");
}

This way you only have a single place to maintain if you need to change the output format for the errors.

这样,如果您需要更改错误的输出格式,那么只需维护一个位置。

#3


4  

The simple way would be to keep a list of errors rather than concatenating them as you go. That would help to maintain a separation of concerns between the logical errors and their presentation.

简单的方法是保存错误列表,而不是在运行时将它们连接起来。这将有助于在逻辑错误和它们的表示之间保持关注点隔离。

See how Spring validation works: you have an Errors object that keeps a list of errors, and a separate message source object that fills in the user-visible messages for the different errors.

查看Spring验证的工作原理:有一个错误对象保存错误列表,还有一个单独的消息源对象,用于填充用户可见的不同错误消息。

#4


4  

Another option is to use Apache Commons StrBuilder, which has the functionality that you're looking for.

另一种选择是使用Apache Commons StrBuilder,它具有您正在寻找的功能。

StrBuilder.appendLn()

StrBuilder.appendLn()

#5


2  

You could try using a PrintStream as it has an println(String string) method which add the new line automatically.

您可以尝试使用PrintStream,因为它有一个println(String String)方法,它会自动添加新行。

Something like this.

是这样的。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
ps.println("Line 1");
ps.println("Line 2");
ps.flush();
String message = new String(bos.toByteArray());
System.out.println(message);

#6


1  

First of all you need to include the newline character(\n) at the end of every .append() yourself:

首先,您需要在每个.append()中包含换行字符(\n):

sb.append("paramX cannot be less than 10 \n");

As for repeating you new-line logic just wrap it in a method:

对于重复你的换行逻辑,用一种方法把它包起来:

public void append(StringBuilder sb, Object value) {
    sb.append(value).append(System.getProperty("line.separator")).append('\n');
}

And use it like:

并使用它:

if(paramX < 10){
    append(sb, "paramX cannot be less than 10");
}

#7


-1  

Just use \n - it will work everywhere. Also, it looks like you want to conditionally add a line feed if there are two messages:

只要用\n -它就可以在任何地方工作。此外,如果有两条消息,看起来您需要有条件地添加一个行提要:

StringBuilder sb = new StringBuilder();

if (paramX<10) {
    sb.append("paramX cannot be less than 10 ");
}

if (paramY<20) {
    if (!sb.length() > 0) // only add newline if needed
        sb.append('\n');
    sb.append("paramY cannot be less than 20 ");
}

#1


-3  

simply append directly...

直接添加…

        if(paramX<10){
            sb.append("paramX cannot be less than 10 \n ");
        }

        if(paramY<20){
            sb.append("paramY cannot be less than 20 \n ");
        }

#2


6  

If you don't want to do it over and over then write a helper method:

如果你不想一遍又一遍地做,那就写一个辅助方法:

public void appendString(StringBuilder builder, String value) {
    builder.append(value + System.getProperty("line.separator"));
}

Then call:

然后调用:

if(paramX<10){
    appendString(sb, "paramX cannot be less than 10 ");
}

This way you only have a single place to maintain if you need to change the output format for the errors.

这样,如果您需要更改错误的输出格式,那么只需维护一个位置。

#3


4  

The simple way would be to keep a list of errors rather than concatenating them as you go. That would help to maintain a separation of concerns between the logical errors and their presentation.

简单的方法是保存错误列表,而不是在运行时将它们连接起来。这将有助于在逻辑错误和它们的表示之间保持关注点隔离。

See how Spring validation works: you have an Errors object that keeps a list of errors, and a separate message source object that fills in the user-visible messages for the different errors.

查看Spring验证的工作原理:有一个错误对象保存错误列表,还有一个单独的消息源对象,用于填充用户可见的不同错误消息。

#4


4  

Another option is to use Apache Commons StrBuilder, which has the functionality that you're looking for.

另一种选择是使用Apache Commons StrBuilder,它具有您正在寻找的功能。

StrBuilder.appendLn()

StrBuilder.appendLn()

#5


2  

You could try using a PrintStream as it has an println(String string) method which add the new line automatically.

您可以尝试使用PrintStream,因为它有一个println(String String)方法,它会自动添加新行。

Something like this.

是这样的。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
ps.println("Line 1");
ps.println("Line 2");
ps.flush();
String message = new String(bos.toByteArray());
System.out.println(message);

#6


1  

First of all you need to include the newline character(\n) at the end of every .append() yourself:

首先,您需要在每个.append()中包含换行字符(\n):

sb.append("paramX cannot be less than 10 \n");

As for repeating you new-line logic just wrap it in a method:

对于重复你的换行逻辑,用一种方法把它包起来:

public void append(StringBuilder sb, Object value) {
    sb.append(value).append(System.getProperty("line.separator")).append('\n');
}

And use it like:

并使用它:

if(paramX < 10){
    append(sb, "paramX cannot be less than 10");
}

#7


-1  

Just use \n - it will work everywhere. Also, it looks like you want to conditionally add a line feed if there are two messages:

只要用\n -它就可以在任何地方工作。此外,如果有两条消息,看起来您需要有条件地添加一个行提要:

StringBuilder sb = new StringBuilder();

if (paramX<10) {
    sb.append("paramX cannot be less than 10 ");
}

if (paramY<20) {
    if (!sb.length() > 0) // only add newline if needed
        sb.append('\n');
    sb.append("paramY cannot be less than 20 ");
}