Java – is there a simple way to concatenate several lines of text into a string without constantly adding line breaks?

So I basically need to do this:

String text = "line1\n";
text += "line2\n";
text += "line3\n";
useString( text );

There is more participation, but this is the basic idea Is there anything I can do more?

DesiredStringThinger text = new DesiredStringThinger();
text.append( "line1" );
text.append( "line2" );
text.append( "line3" );
useString( text.toString() );

Obviously, it doesn't need to work like this, but I think I got the basic point There is always an option to write a loop that handles text by itself, but if there is a standard Java class, I have already done such a thing instead of running a class between applications. I can do such trivial things well

thank you!

Solution

You can use a stringwriter wrapped with a stringwriter:

StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter,true);
writer.println("line1");
writer.println("line2");
writer.println("line3");
useString(stringWriter.toString());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>