Padding a swift String for printing

Droopycom picture Droopycom · Sep 1, 2015 · Viewed 19.4k times · Source

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

Gustavo Seidler picture Gustavo Seidler · Aug 29, 2016

In Swift 3 you can use:

let str = "Test string"
let paddedStr = str.padding(toLength: 20, withPad: " ", startingAt: 0)

Result string: "Test string "

If you need to pad to the left the text (right justify), you can write the following function as an extension to String:

extension String {
    func leftPadding(toLength: Int, withPad character: Character) -> String {
        let newLength = self.characters.count
        if newLength < toLength {
            return String(repeatElement(character, count: toLength - newLength)) + self
        } else {
            return self.substring(from: index(self.startIndex, offsetBy: newLength - toLength))
        }
    }
}

So if you write:

let str = "Test string"
let paddedStr = str.leftPadding(toLength: 20, withPad: " ")

Result string: " Test string"

In Swift 4.1 the substring method is deprecated and there are a number of new methods to obtain a substring. Either prefix, suffix or subscripting the String with a Range<String.Index>.

For the previous extension we can use the suffix method to accomplish the same result. Since the suffix method returns a String.SubSequence, it needs to be converted into a String before being returned.

extension String {
    func leftPadding(toLength: Int, withPad character: Character) -> String {
        let stringLength = self.count
        if stringLength < toLength {
            return String(repeatElement(character, count: toLength - stringLength)) + self
        } else {
            return String(self.suffix(toLength))
        }
    }
}