在java中连接自动生成的字符串,中间有一个空格分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16285257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate auto generated strings in java with a space seperator in between
提问by user894795
I have a string array variable which values changes continuously. Random arrays are generated from it. This is what i have:
我有一个字符串数组变量,它的值不断变化。从中生成随机数组。这就是我所拥有的:
String trans = Utility.GetColumnValue(testdata[k], "suggest_text_2");
The trans
value changes continuously. How can i concatenate it with the previous values? How can i print every value of trans
as it changes continuously? Do i need to use any buffer?
该trans
值连续变化。如何将它与以前的值连接起来?我如何打印trans
不断变化的每个值?我需要使用任何缓冲区吗?
采纳答案by Marco
If you need the intermediate results, you will probably need something like this:
如果你需要中间结果,你可能需要这样的东西:
String yourOldString;
String freshString;
// other code which updates freshString
yourOldString = yourOldString + " " + freshString;
However if you do need to catch all updates but only print out the final result, use a StringBuilder:
但是,如果您确实需要捕获所有更新但只打印出最终结果,请使用 StringBuilder:
private static final String WHITESPACE = " ";
String yourOldString;
String freshString;
StringBuilder builder = new StringBuilder();
builder.append(yourOldString);
// other code which updates freshString
builder.append(WHITESPACE);
builder.append(freshString);
// once everything is done:
String resultString = builder.toString();
回答by Tdorno
System.out.println("string1 "+"string2");
回答by nook
String a = "foo";
String space = " ";
String b = "bar";
String c = a+space+b;
回答by Marvo
It's often best to use StringBuilder to concatenate strings:
通常最好使用 StringBuilder 来连接字符串:
String [] array { "fee", "fie", "foe", "fum" };
boolean firstTime = true;
StringBuilder sb = new StringBuilder(50);
for (String word : array) {
if (firstTime) {
firstTime = false;
} else {
sb.append(' ');
}
sb.append(word);
}
String finalResult = sb.toString();
回答by Blasanka
Simply,
简单地,
String trans = Utility.GetColumnValue(testdata[k], "suggest_text_2");
StringBuffer concat = new StringBuffer();
concat.append(test).append(" ");
Or,
或者,
StringBuffer concat = new StringBuffer();
concat.append(Utility.GetColumnValue(testdata[k], "suggest_text_2")).append(" ");
To concatenate/combine set of data.
连接/组合数据集。
for(String s: trans){
concat.append(s).append(" ");
}
String
concatenation(like trans + " "
) is slower than the StringBuffer
append()
. I strongly suggest you to use StringBuilder
. Because when combinig String
on the fly StringBuffer
is created and then using toString()
converts to String
.
String
连接(如trans + " "
)比StringBuffer
append()
. 我强烈建议你使用StringBuilder
. 由于combinig当String
在飞行中StringBuffer
被创建,然后使用toString()
皈依String
。
Here is nice blog post to read to learn about performance of these two.
这是不错的博客文章,可以阅读以了解这两者的性能。