Padding a swift String for printing

前端 未结 6 659
迷失自我
迷失自我 2020-12-08 09:57

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

6条回答
  •  無奈伤痛
    2020-12-08 10:18

    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.

    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))
            }
        }
    }
    

提交回复
热议问题