有没有一种简单的方法来连接几行文字转换成字符串,而不附加不断换行? [英] Is there an easy way to concatenate several lines of text into a string without constantly appending a newline?

查看:233
本文介绍了有没有一种简单的方法来连接几行文字转换成字符串,而不附加不断换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我基本上是需要做到这一点:

So I essentially need to do this:

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

有更多参与,但是这是基本的想法。是那里有什么可能让我做沿着这虽然线的东西多吗?

There is more involved, but that's the basic idea. Is there anything out there that might let me do something more along the lines of this though?

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

显然,它并不需要精确的工作这样的,但我觉得我得到跨越基点。总有写一个循环处理文本自己的选择,但是这将是很好,如果有一个标准的Java类,在那里,已经做这样的事情,而不是我需要随身携带的一类应用程序之间,这样我就可以做点什么那么微不足道。

Obviously, it does not need to work exactly like that, but I think I get the basic point across. There is always the option of writing a loop which processes the text myself, but it would be nice if there is a standard Java class out there that already does something like this rather than me needing to carry a class around between applications just so I can do something so trivial.

谢谢!

推荐答案

您可以使用的 的StringWriter 包裹在一个<一个href=\"http://java.sun.com/javase/6/docs/api/java/io/PrintWriter.html\"><$c$c>PrintWriter:

You can use a StringWriter wrapped in a PrintWriter:

StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter, true);
writer.println("line1");
writer.println("line2");
writer.println("line3");
useString(stringWriter.toString());

这篇关于有没有一种简单的方法来连接几行文字转换成字符串,而不附加不断换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆