Droopycom Droopycom - 1 year ago 126
Swift Question

Padding a swift String for printing

I'm trying to print a list of Strings all padded to the same width.

In C, I would use something like

printf("%40s", cstr),
where cstr is a C string.

In Swift, the best I could come up is this:

line += String(format: "%40s",string.cStringUsingEncoding(<someEncoding>))

Is there a better way ?

Answer Source

NSString has the stringByPaddingToLength: method:

line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download