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

后端 未结 8 1746
时光说笑
时光说笑 2020-12-15 16:25

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 = ne         


        
8条回答
  •  孤城傲影
    2020-12-15 17:08

    Just add it to your own...

    public static String generateRepeatingString(char c, Integer n) {
        StringBuilder b = new StringBuilder();
        for (Integer x = 0; x < n; x++)
            b.append(c);
        return b.toString();
    }
    

    Or Apache commons has a utility class you can add.

提交回复
热议问题