Simple way to repeat a string

后端 未结 30 3035
清酒与你
清酒与你 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:24

    Here's a way to do it using only standard String functions and no explicit loops:

    // create a string made up of  n  copies of  s
    repeated = String.format(String.format("%%%ds", n), " ").replace(" ",s);
    

提交回复
热议问题