Java 删除 StringBuilder 的最后一个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3395286/
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
Remove last character of a StringBuilder?
提问by Matthew
When you have to loop through a collection and make a string of each data separated by a delimiter, you always end up with an extra delimiter at the end, e.g.
当你必须遍历一个集合并制作一个由分隔符分隔的每个数据的字符串时,你总是在最后得到一个额外的分隔符,例如
for (String serverId : serverIds) {
sb.append(serverId);
sb.append(",");
}
Gives something like : serverId_1, serverId_2, serverId_3,
给出类似:serverId_1, serverId_2, serverId_3,
I would like to delete the last character in the StringBuilder (without converting it because I still need it after this loop).
我想删除 StringBuilder 中的最后一个字符(不转换它,因为在这个循环之后我仍然需要它)。
采纳答案by Jon Skeet
Others have pointed out the deleteCharAt
method, but here's another alternative approach:
其他人已经指出了该deleteCharAt
方法,但这是另一种替代方法:
String prefix = "";
for (String serverId : serverIds) {
sb.append(prefix);
prefix = ",";
sb.append(serverId);
}
Alternatively, use the Joiner
class from Guava:)
As of Java 8, StringJoiner
is part of the standard JRE.
从 Java 8 开始,它StringJoiner
是标准 JRE 的一部分。
回答by bragboy
if(sb.length() > 0){
sb.deleteCharAt(sb.length() - 1);
}
回答by Stephen C
Another simple solution is:
另一个简单的解决方案是:
sb.setLength(sb.length() - 1);
A more complicated solution:
一个更复杂的解决方案:
The above solution assumes that sb.length() > 0
... i.e. there is a "last character" to remove. If you can't make that assumption, and/or you can't deal with the exception that would ensue if the assumption is incorrect, then check the StringBuilder's length first; e.g.
上述解决方案假设sb.length() > 0
......即有一个“最后一个字符”要删除。如果您无法做出该假设,和/或您无法处理假设不正确而导致的异常,则首先检查 StringBuilder 的长度;例如
// Readable version
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
}
or
或者
// Concise but harder-to-read version of the above.
sb.setLength(Math.max(sb.length() - 1, 0));
回答by Antoine
StringBuilder sb = new StringBuilder();
sb.append("abcdef");
sb.deleteCharAt(sb.length() - 1);
assertEquals("abcde",sb.toString());
// true
回答by Zaki
Alternatively,
或者,
StringBuilder result = new StringBuilder();
for(String string : collection) {
result.append(string);
result.append(',');
}
return result.substring(0, result.length() - 1) ;
回答by Jason Day
Yet another alternative:
还有一个选择:
public String join(Collection<String> collection, String seperator) {
if (collection.isEmpty()) return "";
Iterator<String> iter = collection.iterator();
StringBuilder sb = new StringBuilder(iter.next());
while (iter.hasNext()) {
sb.append(seperator);
sb.append(iter.next());
}
return sb.toString();
}
回答by Rafiq
Another alternative
另一种选择
for(String serverId : serverIds) {
sb.append(",");
sb.append(serverId);
}
sb.deleteCharAt(0);
回答by Rohit Reddy Korrapolu
In this case,
在这种情况下,
sb.setLength(sb.length() - 1);
is preferable as it just assign the last value to '\0'
whereas deleting last character does System.arraycopy
更可取,因为它只是将最后一个值分配给'\0'
而删除最后一个字符System.arraycopy
回答by ArtOfWarfare
As of Java 8, the String class has a static method join
. The first argument is a string that you want between each pair of strings, and the second is an Iterable<CharSequence>
(which are both interfaces, so something like List<String>
works. So you can just do this:
从 Java 8 开始, String 类有一个静态方法join
。第一个参数是你想要的每对字符串之间的一个字符串,第二个参数是一个Iterable<CharSequence>
(它们都是接口,所以类似的List<String>
工作。所以你可以这样做:
String.join(",", serverIds);
Also in Java 8, you could use the new StringJoiner
class, for scenarios where you want to start constructing the string before you have the full list of elements to put in it.
同样在 Java 8 中,您可以使用新StringJoiner
类,用于您想要在将完整的元素列表放入字符串之前开始构造字符串的场景。
回答by Vikasdeep Singh
I am doing something like below:
我正在做类似下面的事情:
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < value.length; i++) {
stringBuilder.append(values[i]);
if (value.length-1) {
stringBuilder.append(", ");
}
}