Simple way to repeat a string

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

    Not the shortest, but (i think) the fastest way is to use the StringBuilder:

     /**
       * Repeat a String as many times you need.
       *
       * @param i - Number of Repeating the String.
       * @param s - The String wich you want repeated.
       * @return The string n - times.
       */
      public static String repeate(int i, String s) {
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < i; j++)
          sb.append(s);
        return sb.toString();
      }
    

提交回复
热议问题