可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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() );
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.
Thanks!
回答1:
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());
回答2:
AFAIK there's no library class that allows you to do so.
The following does the work though:
class DesiredStringThinger { StringBuilder text = new StringBuilder(); public void append(String s) { text.append(s).append("\n"); } @Override public String toString() { return text.toString(); } }
回答3:
public String createString () { StringBuilder sb = new StringBuilder (); String txt = appendLine("firstline", sb).appendLine("2ndLine", sb).toString(); } private StringBuilder appendLine (String line, StringBuilder sb) { String lsp = System.getProperty("line.separator"); return sb.append (line).append (lsp); }
回答4:
You can use from Apache Commons the StringUtils.join helper. Which allows to build a String from a list. You can add the 'delimiter' character/string.
回答5:
If you are willing to use external libraries, check out the Joiner in Guava.
Your code would go to something like
String result = Joiner.on("\n").join(parts);
where parts
is an Iterable<String>
.
回答6:
You can use a StringBuffer
StringBuffer text = new StringBuffer(); text.append("line1"); text.append("line2"); ... useString(text.toString());
This will not append the new line character, but you can certainly append that as well for each line.
回答7:
Perhaps the lowest impact method is to add a static method to append with a new line to a StringBuilder
.
public static StringBuilder appendln(StringBuilder buff, String str) { return buff.append(str).append('\n'); }
But @Joachim Sauer
beats me to my preferred solution. For more complex examples you might want to use your own Writer
decorator, as @Rahul G
(only use private fields).
回答8:
If you are not crazy about performance, I think this is clean and neat.
class DesiredStringThinger { StringBuilder sb = new StringBuilder(); public void concat(String... s) { for(String str : s){ sb.append(s).append("\n"); } } @Override public String toString() { return sb.toString(); } }