Simple way to repeat a string

后端 未结 30 3135
清酒与你
清酒与你 2020-11-21 06:55

I\'m looking for a simple commons method or operator that allows me to repeat some string n times. I know I could write this using a for loop, but I wish to avoid f

30条回答
  •  忘掉有多难
    2020-11-21 07:18

    public static String repeat(String str, int times) {
        int length = str.length();
        int size = length * times;
        char[] c = new char[size];
        for (int i = 0; i < size; i++) {
            c[i] = str.charAt(i % length);
        }
        return new String(c);
    }
    

提交回复
热议问题