tags:

views:

373

answers:

8

I did check the other questions; this question has its focus on solving this particular question the most efficient way.

Sometimes you want to create a new string with a specified length, and with a default character filling the entire string.

ie, it would be cool if you could do new String(10, '*') and create a new String from there, with a length of 10 characters all having a *.

Because such a constructor does not exist, and you cannot extend from String, you have either to create a wrapper class or a method to do this for you.

At this moment I am using this:

protected String getStringWithLengthAndFilledWithCharacter(int length, char charToFill) {
    char[] array = new char[length];
    int pos = 0;
    while (pos < length) {
        array[pos] = charToFill;
        pos++;
    }
    return new String(array);
}

It still lacks any checking (ie, when length is 0 it will not work). I am constructing the array first because I believe it is faster than using string concatination or using a StringBuffer to do so.

Anyone else has a better sollution?

+3  A: 

Simply use the StringUtils class from apache commons lang project. You have a leftPad method:

StringUtils.leftPad("foobar", 10, '*'); // Returns "****foobar"
romaintaz
+1 - that's exactly what he was looking for (aka 'cool'). For the exact same behaviour, but that's obvious, do: `StringUtils.leftPad("", 10, '*');`
Andreas_D
+1 nice solution, though I wouldn't add a jar to my project just for that.
abyx
Thanks. Yes that looks like a more suitable way to use it. Luckily I am already using this jar, so i could use this straight away.
Stefan Hendriks
Or use repeat(), which doesn't require the empty string at the start and is arguably clearer in intent (see my answer)
Cowan
@abyx> Yes, you will need to add a JAR, but commons lang offers so nice utils methods, so I don't think it is really a drawback!
romaintaz
+12  A: 

No need to do the loop, and using just standard Java library classes:

protected String getStringWithLengthAndFilledWithCharacter(int length, char charToFill) {
  if (length > 0) {
    char[] array = new char[length];
    Arrays.fill(array, charToFill);
    return new String(array);
  }
  return "";
}

As you can see, I also added suitable code for the length == 0 case.

unwind
won't the code in Arrays.fill() contain a loop...
NomeN
Maybe, but 1) it's likely to be faster and 2) that's code you don't need to write.
abyx
Good find. Yes Arrays.fill does a for loop itself. But fundementally this is better.
Stefan Hendriks
You should throw an IllegalArgumentException if (length < 0). (Even java.lang.NegativeArraySizeException is better.)
Thomas Jung
+1  A: 
public static String fillString(int count,char c) {
    StringBuilder sb = new StringBuilder( count );
    for( int i=0; i<count; i++ ) {
        sb.append( c ); 
    }
    return sb.toString();
}

What is wrong?

Alexander Pavloshchuk
A: 

To improve performance you could have a single predefined sting if you know the max length like:

String template = "####################################";

And then simply perform a substring once you know the length.

Karl

Karl
+1: I just read on another post that substring creates a String that just uses the same char[] of the original String. So it's an O(1) operation (no loop) and it saves memory (if that is a real issue)
Andreas_D
+7  A: 

Apache Commons Lang (probably useful enough to be on the classpath of any non-trivial project) has StringUtils.repeat():

String filled = StringUtils.repeat("*", 10);

Easy!

Cowan
+1  A: 

The above is fine. Do you mind if I ask you a question - Is this causing you a problem? It seams to me you are optimizing before you know if you need to.

Now for my over engineered solution. In many (thou not all) cases you can use CharSequence instead of a String.

public class OneCharSequence implements CharSequence {
  private final char value;
  private final char length;
  public OneCharSequence(final char value, final char length) {
    this.value = value;
    this.length = length;
  }
  public char   charAt(int index)  {
     if(index < length) return value;
     throw new IndexOutOfBoundsException();
  }
  public int length() {
    return length;
  }
  public CharSequence subSequence(int start, int end) {
     return new OneCharSequence(end-start, value);
  }
  public String toString() {
    char[] array = new char[length];
    Arrays.fill(array, value);
    return new String(array);
  }
}
mlk
A: 

One extra note: it seems that all public ways of creating a new String instance involves necessarily the copy of whatever buffer you are working with, be it a char[], a StringBuffer or a StringBuilder. From the String javadoc (and is repeated in the respective toString methods from the other classes):

The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

So you'll end up having a possibly big memory copy operation after the "fast filling" of the array. The only solution that may avoid this issue is the one from @mlk, if you can manage working directly with the proposed CharSequence implementation (what may be the case).

PS: I would post this as a comment but I don't have enough reputation to do that yet.

Chuim
A: 

using Dollar is simple:

String filled = $("=").repeat(10).toString(); // produces "========="
dfa
There are 9 `=` in the comment.
True Soft