开发者

Converting from Integer value to StringBuffer with zeros as the default value

开发者 https://www.devze.com 2023-04-04 04:42 出处:网络
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

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 St开发者_C百科ringBuffer but seems to be very inefficient method. Please guide me.


int numZeroes = 5;
StringBuilder sb = new StringBuilder();

for (int i = 0; i < numZeroes; i++)
{
    sb.append('0');
}


Here's a utility method you could use:

public StringBuilder repeatChars(StringBuilder sb, char c, int n) {
    for (int i = 0; i < n; ++i) {
        sb.append(c);
    }

    return sb;
}

Call it like this:

StringBuilder sb = new StringBuilder();

// Add nine zeroes
repeatChars(sb, '0', 9);

Note: I made it return the StringBuilder so that you can chain method calls.

It's better to use StringBuilder instead of StringBuffer, because StringBuilder doesn't have the unnecessary synchronization that StringBuffer 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).


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 a StringBuilder instead.

What's wrong with a trivial loop:

StringBuilder buf = new StringBuilder(length); // might want to use more if you know that you'll append something
for (int i = 0; i < length; i++) {
  buf.append('0');
}

And if you know the maximum number of zeroes to use, then you might do this:

// make sure never to change the content of this
private static final char[] ZEROES = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0' };

// when you need the zeroes do this:
StringBuilder buf = new StringBuilder(length);
buf.append(ZEROES, 0, length);


I'm not sure there's anything to do it in a single line, but you can use:

StringBuffer buffer = new StringBuffer(length);
for (int i = 0; i < length; i++)
{
    buffer.append('0');
}

(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 than StringBuffer, 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.


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:

StrBuilder b = new StrBuilder();
b.appendPadding(9, '0').append(...).append(...)....
0

精彩评论

暂无评论...
验证码 换一张
取 消