你如何填充 StringBuilder?

How do you pad the StringBuilder?

我构建了一个字符串,在末尾添加字符并顺便修剪末尾。顺便说一下,我需要在最后添加 n 个相同的 c-chars。你是怎么做到的,1 到 n sb.append(c)?

来自the documentation for StringBuilder

def padTo(len: Int, elem: A): IndexedSeq[A]

[use case]
A copy of this string builder with an element value appended until a given target length is reached.

len the target length
elem the padding value

returns a new string builder consisting of all elements of this string builder followed by the minimal number of occurrences of elem so that the resulting string builder has a length of at least len.

如果你真的需要添加另一个 n 个字符(通常不是 pad 的意思,那么使用 len 作为 sb.length + n