0

I build a string adding chars to the end and trimming the end incidentially. Incidentially, I need to add n identical c-chars to the end. How do you do that, 1 to n sb.append(c)?

1 Answers1

2

From 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.

If you really need to add another n characters (not usually what's meant by pad, then use len as sb.length + n

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134