Simple way to repeat a string

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

    Here is the shortest version (Java 1.5+ required):

    repeated = new String(new char[n]).replace("\0", s);
    

    Where n is the number of times you want to repeat the string and s is the string to repeat.

    No imports or libraries needed.

提交回复
热议问题