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

后端 未结 8 1740
时光说笑
时光说笑 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:04

    If you can, use StringUtils from Apache Commons Lang:

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

提交回复
热议问题