本文翻译自:Remove last character of a StringBuilder?
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, eg 当你必须遍历一个集合并用分隔符分隔每个数据的字符串时,你最终总会得到一个额外的分隔符,例如
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中的最后一个字符(没有转换它,因为我在这个循环之后仍然需要它)。
#1楼
参考:https://stackoom.com/question/EFGg/删除StringBuilder的最后一个字符
#2楼
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
#3楼
As of Java 8, the String class has a static method join
. 从Java 8开始,String类具有静态方法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: 第一个参数是你想要在每对字符串之间的字符串,第二个是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
类,以便在您拥有要放入的完整元素列表之前开始构建字符串。
#4楼
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(", ");
}
}
#5楼
Just get the position of the last character occurrence. 只需获取最后一个字符出现的位置。
for(String serverId : serverIds) {
sb.append(serverId);
sb.append(",");
}
sb.deleteCharAt(sb.lastIndexOf(","));
Since lastIndexOf
will perform a reverse search, and you know that it will find at the first try, performance won't be an issue here. 由于lastIndexOf
将执行反向搜索,并且您知道它会在第一次尝试时找到,因此性能不会成为问题。
EDIT 编辑
Since I keep getting ups on my answer (thanks folks 😊), it is worth regarding that: 由于我一直在接受我的回答(感谢大家😊),因此值得考虑:
On Java 8 onward it would just be more legible and explicit to use StringJoiner . 在Java 8之后,使用StringJoiner会更加清晰明了 。 It has one method for a simple separator, and an overload for prefix and suffix. 它有一个简单分隔符的方法,以及前缀和后缀的重载。
Examples taken from here: example 从这里取的例子 : 例子
Example using simple separator: 示例使用简单分隔符:
StringJoiner mystring = new StringJoiner("-"); // Joining multiple strings by using add() method mystring.add("Logan"); mystring.add("Magneto"); mystring.add("Rogue"); mystring.add("Storm"); System.out.println(mystring);
Output: 输出:
Logan-Magneto-Rogue-Storm 洛根 - 磁盗贼风暴
Example with suffix and prefix: 带后缀和前缀的示例:
StringJoiner mystring = new StringJoiner(",", "(", ")"); // Joining multiple strings by using add() method mystring.add("Negan"); mystring.add("Rick"); mystring.add("Maggie"); mystring.add("Daryl"); System.out.println(mystring);
Output 产量
(Negan,Rick,Maggie,Daryl) (Negan,瑞克,麦琪,达里尔)
#6楼
sb.deleteCharAt(sb.length() - 1)