2

is there a more efficient way to generate a String full of blanks with a customizable size like the following:

private String getBlanks (int numberOfBlanks)
    {
        String theBlanks = "";
        for (int i = 0; i < numberOfBlanks; i++)
        {
            theBlanks = theBlanks + " ";
        }
        return theBlanks;
    }

Perhaps with a StringBuilder or any other type?

EDIT: Since we have Appache Commons Lang the most convinient way of doing is through the use of String Utils - leftPad, thanks everyone for the answers!

Thanks

4
  • Efficient in what way? Coding complexity or execution time? Either way, the question is almost moot in terms of why it would need to be made more efficient. Commented May 10, 2011 at 15:07
  • I disagree on this one. Though it wouldn't be a horrible crime againt humanity to do it this way, it's not at all tidy to manipulate Strings this way. Amines answer below is (IMO) the way to do it. Commented May 10, 2011 at 15:10
  • @Tejs referring more to execution time Commented May 10, 2011 at 15:13
  • On a side note, consider using StrBuilder (part of apache commons lang) instead of StringBuilder. it is a drop in replacement and the apache folks claim it is better (in general I take them at their word). Commented May 10, 2011 at 15:26

7 Answers 7

7

Maybe like this:

private String getBlanks(int numberOfBlanks) {
       char[] chars = new char[numberOfBlanks];
       Arrays.fill(chars, (char)32);
       return new String(chars);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Very clever (and some really long phrase so that I can publish it).
5

With Guava

String blanks = Strings.repeat(" ", numOfblanks);

Comments

4

Quick hack using StringUtils:

return StringUtils.leftPad("", numberOfBlanks, ' ');

2 Comments

Oh, I forgot to say, you need Apache Commons Lang for this
We have Appache Commons and I find this way the most efficient :)
2

First of all - use a StringBuilder, and then return StringBuilder.toString

Comments

2

You want a StringBuilder:

private String getBlanks (int numberOfBlanks)
    {
        StringBuilder theBlanks = new StringBuilder(numberOfBlanks);
        for (int i = 0; i < numberOfBlanks; i++) theBlanks.append(" ");
        return theBlanks.toString();
    }

Comments

2
String getBlankString(int length){
  StringBuffer sb=new StringBuffer(length);

  for(int i=0;i<length;i++){
    sb.append(" ");
  }
  return sb.toString();
}

1 Comment

StringBuffer is thread safe, it's necessary only in specific cases, otherwise it is only adding additional locking.
1
private String getBlanks (int numberOfBlanks) {
    StringBuilder sb = new StringBuilder();
    sb.setLength(numberOfBlanks);
    sb.replace(0, numberOfBlanks-1, " ");

    return sb.toString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.