Java deleteCharAt 或 setLength,哪种方法更好地从 StringBuilder/StringBuffer 中删除最后一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21645559/
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
deleteCharAt or setLength, which way is better to remove last char from a StringBuilder/StringBuffer
提问by Weibo Li
In many cases, we need to delete the last char of a StringBuilder/StringBuffer. For example, given a int[]{1,2,3}
, to implement a String toString(int[] a)
method, contacting each elements with a comma separator. The output should be 1,2,3
, no tailing comma.
在很多情况下,我们需要删除 StringBuilder/StringBuffer 的最后一个字符。例如,给定一个int[]{1,2,3}
, 来实现一个String toString(int[] a)
方法,用逗号分隔符联系每个元素。输出应该是1,2,3
, 没有尾随逗号。
We can easily write a loop:
我们可以轻松地编写一个循环:
int[] nums = new int[]{1,2,3,4,5};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.length; i++) {
sb.append(nums[i]);
sb.append(",");
}
//here we need to remove the tailing ','
but always we need to remove the tailing ','
. There are two ways to implement it:
但我们总是需要去除尾矿','
。有两种实现方式:
sb.deleteCharAt(sb.length() - 1);
and
和
sb.setLength(sb.length() - 1);
Which one is recommended? Why?
推荐哪一款?为什么?
NOTE:
I know what does Arrays.toString
do. That's just an example to describe my question, maybe not quite proper.
This is not a discussion about strings concatenation but the best practices of StringBuffer/StringBuilder.
注意:我知道做Arrays.toString
什么。这只是描述我的问题的一个例子,可能不太合适。
这不是关于字符串连接的讨论,而是 StringBuffer/StringBuilder 的最佳实践。
采纳答案by ???v?т?
Actually, there is very little in it and is probably dependent on hardware and other factors.
实际上,里面的内容很少,可能取决于硬件和其他因素。
The setLength()
method simply alters the count and overwrites the unwanted value in the array with a zero byte.
该setLength()
方法只是改变计数并用零字节覆盖数组中不需要的值。
The deleteCharAt()
performs an array copy internally, before altering the count. That sounds dramatic, but the array being copied is actually zero-length because you're removing the last character.
的deleteCharAt()
执行的阵列内部复制,改变计数之前。这听起来很戏剧性,但被复制的数组实际上是零长度的,因为您要删除最后一个字符。
I would recommend going for setLength()
as it is shorter to type and I think makes it clearer what you are doing. If performance is an issue and, on measuring, you find this is the bottleneck for you, then perhaps you could consider a different algorithm that doesn't require changing the size (as per JB Nizet's answer).
我建议你去,setLength()
因为它打字更短,我认为让你更清楚你在做什么。如果性能是一个问题,并且在测量时发现这对您来说是瓶颈,那么也许您可以考虑一种不需要更改大小的不同算法(根据 JB Nizet 的回答)。
回答by JB Nizet
I wouldn't do it like this. Instead, I would only add a trailing comma if the element is not the last element of the array. Or I would use Guava's Joiner (or Apache-commons StringUtils), which makes it much clearer:
我不会这样做。相反,如果元素不是数组的最后一个元素,我只会添加一个尾随逗号。或者我会使用 Guava 的 Joiner(或 Apache-commons StringUtils),这使它更清晰:
String s = Joiner.on(',').join(nums);
NB: I just noticed that Guava's Joiner doesn't deal with primitive arrays. You should get the idea from the above anyway.
注意:我刚刚注意到 Guava 的 Joiner 不处理原始数组。无论如何,你应该从上面得到这个想法。
回答by Bohemian
How you do it properly is to conditionally prepend the comma:
你如何正确地做到这一点是有条件地添加逗号:
for (int i = 0; i < nums.length; i++) {
if (i > 0)
sb.append(',');
sb.append(nums[i]);
}
Then you don't need to worry about removing the last character, because it's already correct.
那么你就不用担心删除最后一个字符了,因为它已经正确了。