What is the easiest way to generate a String of n repeated characters?

40,304

Solution 1

int n = 10;
char[] chars = new char[n];
Arrays.fill(chars, 'c');
String result = new String(chars);

EDIT:

It's been 9 years since this answer was submitted but it still attracts some attention now and then. In the meantime Java 8 has been introduced with functional programming features. Given a char c and the desired number of repetitions count the following one-liner can do the same as above.

String result = IntStream.range(1, count).mapToObj(index -> "" + c).collect(Collectors.joining());

Do note however that it is slower than the array approach. It should hardly matter in any but the most demanding circumstances. Unless it's in some piece of code that will be executed thousands of times per second it won't make much difference. This can also be used with a String instead of a char to repeat it a number of times so it's a bit more flexible. No third-party libraries needed.

Solution 2

If you can, use StringUtils from Apache Commons Lang:

StringUtils.repeat("ab", 3);  //"ababab"

Solution 3

Google Guava Time!

Strings.repeat("a", 3)

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html

Solution 4

To have an idea of the speed penalty, I have tested two versions, one with Array.fill and one with StringBuilder.

public static String repeat(char what, int howmany) {
    char[] chars = new char[howmany];
    Arrays.fill(chars, what);
    return new String(chars);
}

and

public static String repeatSB(char what, int howmany) {
    StringBuilder out = new StringBuilder(howmany);
    for (int i = 0; i < howmany; i++)
        out.append(what);
    return out.toString();
}

using

public static void main(String... args) {
    String res;
    long time;

    for (int j = 0; j < 1000; j++) {
        res = repeat(' ', 100000);
        res = repeatSB(' ', 100000);
    }
    time = System.nanoTime();
    res = repeat(' ', 100000);
    time = System.nanoTime() - time;
    System.out.println("elapsed repeat: " + time);

    time = System.nanoTime();
    res = repeatSB(' ', 100000);
    time = System.nanoTime() - time;
    System.out.println("elapsed repeatSB: " + time);
}

(note the loop in main function is to kick in JIT)

The results are as follows:

elapsed repeat  : 65899
elapsed repeatSB: 305171

It is a huge difference

Solution 5

Java SE 11

String#repeat​(int count), introduced as part of Java SE 11, makes it quite easy to do.

Demo:

public class Main {
    public static void main(String[] args) {
        char ch = 'c';
        int n = 20;
        String result = String.valueOf(ch).repeat(n);
        System.out.println(result);
    }
}

Output:

cccccccccccccccccccc
Share:
40,304
foobar
Author by

foobar

Updated on April 27, 2021

Comments

  • foobar
    foobar about 3 years

    Given a character c and a number n, how can I create a String that consists of n repetitions of c? Doing it manually is too cumbersome:

    StringBuilder sb = new StringBuilder(n);
    for (int i = 0; i < n; ++i)
    {
        sb.append(c);
    }
    String result = sb.toString();
    

    Surely there is some static library function that already does this for me?

  • Jesper
    Jesper over 12 years
    Why are you using Integer instead of int?
  • G_H
    G_H over 12 years
    It's in single quotes, not a variable. Unless I'm missing something here... I'm a bit groggy today.
  • Tedil
    Tedil over 12 years
    I'm talking about the c in the original question, not in your answer
  • foobar
    foobar over 12 years
    @Tedil: "Given a character c and a number n [...]"
  • Mike Thomsen
    Mike Thomsen over 12 years
    Personal preference. It doesn't really matter.
  • G_H
    G_H over 12 years
    Ah, right. Should also work for a Character object thanks to auto-unboxing. I'm assuming it's a char or Character.
  • user85421
    user85421 over 12 years
    @Mike - doesn't matter? Using your own words "That's very wasteful. Every iteration of that for loop will" call intValue (3 times) and valueOf (once/interaction) eventually creating a new Integer (x > 127)!
  • Tsuyoshi Ito
    Tsuyoshi Ito almost 10 years
    This takes O(log N) time assuming that += for String takes O(1) time. I do not think that your assumption is reasonable.
  • rossum
    rossum almost 10 years
    @Tsuyoshi Ito: Good point. Using a pre-sized StringBuilder will get closer to the ideal.
  • Matthieu
    Matthieu about 8 years
    @TsuyoshiIto You use O() according to a unit operation you define. In that case, += is the unit operation so O(log N) is valid. But you're right pointing that, internally, += will be using a StringBuilder which can have an overhead.
  • Kris
    Kris over 7 years
    Seems like this involves copying longer and longer strings on each successive +=. So this algo does more work on each += than the previous. It also creates garbage. So using '+=' as the 'unit' is really not such a good way of looking at it. You are probably better of using a single StringBuilder and loop to add chars one by one. A lot less garbage and string copying will be involved which probably means better performance overall.
  • rossum
    rossum over 7 years
    See my response to @Tsuyoshi Ito. A pre-sized StringBuilder will remove a lot of the overhead by allocating all the space needed at the start.
  • Craig P. Motlin
    Craig P. Motlin over 6 years
    Your testing methodology is flawed. Use JMH for accurate results.
  • Panayotis
    Panayotis over 6 years
    Indeed this is not a perfect benchmark (a perfect benchmark simply doesn't exist) buy it clearly shows how slower the one method is compared with the other.
  • G_H
    G_H over 6 years
    There is no way to do this in O(log N). If you want to have a string of n repeating characters, it means that n memory locations will have to be filled in with a value. This is O(N) at best. Your algorithm is string concatenation in a loop in disguise. Using a StringBuilder will eventually still come down to linear time at best.
  • Holger
    Holger over 3 years
    The Stream approach is indeed not an advantage. But since Java 11, you can use (""+c).repeat(n)
  • G_H
    G_H almost 3 years
    It is not a huge difference. In relative terms, sure, the second method is almost 5 times as slow. But we're looking at the difference between something that takes about 65 _micro_seconds and something that takes 305 _micro_seconds. It's not even in the millisecond scale. And that's for making a 100000 character String. I would never make a choice based on such microscopic optimizations instead of legibility in Java. Only with the exception if it concerns a piece of code that will be executed many times per second.
  • G_H
    G_H almost 3 years
    Useful to know: commons-lang3 has a method taking a char instead of a String. The implementation in recent versions effectively does the same as my answer (using Arrays.fill). The version taking a String has a bunch of branching code paths for optimizations. If the input String is of length 1 it ends up calling the method taking a char. The versions before commons-lang3 seem to perform well too. If you happen to have commons-lang on your classpath you may as well use it.