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 ?

问题评论:

答案:

答案1:

NSString has the stringByPaddingToLength: method:

line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)

答案评论:

1
For add padding to left side ,we have to write our own code., extension String { func PadLeft(totalWidth: Int,byString:String) -> String { let toPad = totalWidth – self.characters.count; if toPad < 1 { return self; } return “”.stringByPaddingToLength(toPad, withString: byString, startingAtIndex: 0) + self; } }

答案2:

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 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.characters.count
        if stringLength < toLength {
            return String(repeatElement(character, count: toLength - stringLength)) + self
        } else {
            return String(self.suffix(toLength))
        }
    }
}

答案评论:

答案3:

Put all string-format-code into extension and reuse it wherever you want.

extension String {
    func padding(length: Int) -> String {
        return self.stringByPaddingToLength(length, withString: " ", startingAtIndex: 0)
    }

    func padding(length: Int, paddingString: String) -> String {
        return self.stringByPaddingToLength(length, withString: paddingString, startingAtIndex: 0)
    }
}

var str = "str"
print(str.padding(10)) // "str       "
print(str.padding(10, paddingString: "+")) // "str+++++++"

答案评论:

Seems these functions strips linefeeds from the output.

原文地址:

https://stackoverflow.com/questions/47750201/swift-4-0-right-padding-a-string

添加评论

友情链接:蝴蝶教程