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.
在C中,我将使用printf(“%40”,cstr),其中cstr是一个C字符串。
In Swift, the best I could come up is this:
在斯威夫特的书中,我能想到的最好的一句话是:
line += String(format: "%40s",string.cStringUsingEncoding(<someEncoding>))
Is there a better way ?
有更好的办法吗?
3 个解决方案
#1
12
NSString
has the stringByPaddingToLength:
method:
NSString有stringByPaddingToLength:方法:
line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)
#2
43
In Swift 3 you can use:
在Swift 3中,您可以使用:
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>
.
在Swift 4中,不赞成使用子字符串方法,并且有许多新方法来获取子字符串。带范围
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
5
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+++++++"
#1
12
NSString
has the stringByPaddingToLength:
method:
NSString有stringByPaddingToLength:方法:
line += string.stringByPaddingToLength(40, withString: " ", startingAtIndex: 0)
#2
43
In Swift 3 you can use:
在Swift 3中,您可以使用:
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>
.
在Swift 4中,不赞成使用子字符串方法,并且有许多新方法来获取子字符串。带范围
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
5
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+++++++"