java 如何在字符串生成器中放置空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35013895/
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
How to put spaces in a stringbuilder
提问by KeinHappyAuer
Hello I have following method to display a promotion line when I comment a shoutbox:
您好,我有以下方法可以在评论留言框时显示促销行:
public String getShoutboxUnderline(){
StringBuilder builder = new StringBuilder();
builder.append("watch");
builder.append("on");
builder.append("youtube");
builder.append(":");
builder.append("Mickey");
builder.append("en");
builder.append("de");
builder.append("stomende");
builder.append("drol");
return builder.toString();
}
But when I get it, I get watchonyoutube:mickeyendestomendedrol, which is without spaces. How do I get spaces in my Stringbuilder?
但是当我得到它时,我得到了 watchonyoutube:mickeyendestomendedrol,它没有空格。如何在我的 Stringbuilder 中获得空格?
回答by Mohammed Aouf Zouag
As of JDK 1.8, you can use a StringJoiner
, which is more convenient in your case:
从 JDK 1.8 开始,您可以使用 a StringJoiner
,这在您的情况下更方便:
StringJoiner
is used to construct a sequence of characters separated by a delimiterand optionallystarting with a supplied prefix and ending with a supplied suffix.
StringJoiner
用于构造由分隔符分隔的字符序列,并且可以选择以提供的前缀开头并以提供的后缀结尾。
StringJoiner joiner = new StringJoiner(" "); // Use 'space' as the delimiter
joiner.add("watch") // watch
.add("on") // watch on
.add("youtube") // watch on youtube
.add(":") // etc...
.add("Mickey")
.add("en")
.add("de")
.add("stomende")
.add("drol");
return joiner.toString();
This way, you will not need to add those spaces "manually".
这样,您就不需要“手动”添加这些空格。
回答by Mena
Just invoke builder.append(" ")
at the location of your preference.
只需builder.append(" ")
在您喜欢的位置调用。
E.g.
例如
builder
.append("watch")
.append(" ")
.append("on")
...etc.
...等等。
NB:
注意:
- Using the fluent builder syntax here for convenience
- You can also just append a space after each literal instead (save for the last one)
- 为方便起见,在此处使用 fluent builder 语法
- 您也可以只在每个文字后附加一个空格(最后一个除外)
回答by abdulrafique
Cleaner way of doing it.
更清洁的方式。
Create a class variable:
创建类变量:
private static final String BLANK_SPACE=" ";
Now in you StringBuilder code ,append it where required:
现在在你的 StringBuilder 代码中,在需要的地方附加它:
StringBuilder builder = new StringBuilder();
builder.append("watch");
builder.append(BLANK_SPACE);
builder.append("on");
builder.append("youtube");
builder.append(":");
builder.append(BLANK_SPACE);
builder.append("Mickey");
builder.append("en");
builder.append("de");
builder.append(BLANK_SPACE);
builder.append("stomende");
builder.append("drol");
System.out.println(builder.toString());
回答by Davide Lorenzo MARINO
A space isonly a string containing the singlecharacter space.
空格只是包含单个字符空格的字符串。
So you can append it exactly as appending any other string.
因此,您可以像附加任何其他字符串一样附加它。
StringBuilder builder = new StringBuilder();
builder.append("watch");
builder.append(" ");
builder.append("on");
builder.append(" ");
// and so on
Remember also that the append method returns the StringBuilder so it is possible to join appends one after the other as follow
还请记住,append 方法返回 StringBuilder,因此可以按如下方式一个接一个地加入附加项
StringBuilder builder = new StringBuilder();
builder.append("watch").append(" ");
builder.append("on").append(" ");
// and so on