Use StringBuilder to pad String with blank spaces or other characters

后端 未结 5 983
别跟我提以往
别跟我提以往 2020-12-17 16:52

I\'m a beginner to java and this is my first post on Stackoverflow. Although my initial code is similar to other posts here, my question relates to implementing StringBuilde

5条回答
  •  隐瞒了意图╮
    2020-12-17 17:28

    You can also something really simpler with the fill() method of Arrays.

    StringBuilder sb = new StringBuilder();
    char[] pad = new char[padnum - result.length()];
    Arrays.fill(pad, '*');
    return sb.append(pad).append(result).toString();
    

    We are in really little times but this method is about 25% faster than the for loop one.

提交回复
热议问题