从 Integer 值转换为 StringBuffer(以零作为默认值)
我正在尝试形成一个初始值为 00000 的 StringBuffer 。
我的问题是初始值取决于整数变量,并且基于整数变量的值,StringBuffer 中应该有许多个零。
例如 int length =9,因此我的 StringBuffer 应该由 000000000...(9 个零)组成...
我没有办法解决这个问题。任何人都可以指导我吗?
我想到的一种方法是获取整数值并将那么多值存储在 String 数组中,然后将字符串数组写入 StringBuffer 但似乎是非常低效的方法。请指导我。
I am trying to form a StringBuffer with an initial value of 00000..
My problem is the initial value is dependent on an integer variable and based on the value of integer variable that many number of zeros should be there in StringBuffer..
E.g. int length=9, therefore my StringBuffer should form with 000000000....(9 zeros)...
I am not getting a way out of this problem. Can anyone please guide me on this.
One way I figured out is by getting the integer value and storing that many values in String array and then writing string array to StringBuffer but seems to be very inefficient method. Please guide me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
下面是您可以使用的实用方法:
像这样调用它:
注意:我让它返回
StringBuilder
以便您可以链接方法调用。最好使用
StringBuilder
而不是StringBuffer
,因为StringBuilder
没有StringBuffer
所具有的不必要的同步。实际上没有比使用循环更有效的方法了。不要太担心微优化(担心简单的语句不够高效)。
Here's a utility method you could use:
Call it like this:
Note: I made it return the
StringBuilder
so that you can chain method calls.It's better to use
StringBuilder
instead ofStringBuffer
, becauseStringBuilder
doesn't have the unnecessary synchronization thatStringBuffer
has.There's really no other, more efficient way than to do this with a loop. Don't worry too much about micro-optimization (worrying that simple statements are not efficient enough).
除非您坚持使用 Java 1.4.2(或更早版本)或者您有非常具体的理由使用
StringBuffer
,否则您几乎应该始终使用StringBuilder
代替。一个简单的循环有什么问题:
如果您知道要使用的最大零数,那么您可以这样做:
Unless you're stuck with Java 1.4.2 (or earlier) or you have a very specific reason to use
StringBuffer
, you should almost always use aStringBuilder
instead.What's wrong with a trivial loop:
And if you know the maximum number of zeroes to use, then you might do this:
我不确定是否可以在一行中执行任何操作,但您可以使用:(
我怀疑附加一个
char
比附加一个字符串稍微更有效,因为不需要进行无效检查,并且长度是已知的。)如果您使用的是 Java 5 或更高版本,通常使用
StringBuilder
比使用StringBuffer 更好。
,由方式,这样你就不会进行不必要的同步。这使用长度作为初始容量,如果您不打算再附加任何内容,那么这很好 - 但如果您以后要附加数据,请适当更改它以提供更大的容量。
I'm not sure there's anything to do it in a single line, but you can use:
(I suspect appending a
char
is marginally more efficient than appending a string, as there's no need for a nullity check and the length is known. It won't be significant though.)If you're using Java 5 or higher, it's generally better to use
StringBuilder
thanStringBuffer
, by the way, so that you don't have unnecessary synchronization.This uses the length as the initial capacity, which is good if you aren't going to append any more - but if you're going to append data afterwards, change it appropriately to give a larger capacity.
迟到的答案可能没有用,但是如果您正在使用 Apache Commons Lang,并且许多重要的 Java 项目都在使用 Apache Commons Lang,那么您可以通过他们的
StrBuilder
获得您想要的东西,特别是它的 < a href="http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/text/StrBuilder.html#appendPadding%28int,%20char%29" rel="nofollow">appendPadding 方法。你可以写:
Late answer and possibly not useful, but if you are using Apache Commons Lang, and many significant Java projects do, you have what you want with their
StrBuilder
, particularly with its appendPadding method.You can write: