Swift:格式字符串宽度

vsaztqbk  于 2022-12-10  发布在  Swift
关注(0)|答案(7)|浏览(146)

在C/C++、Java和许多其他语言中,我想做的事情非常简单,我所要做的就是能够指定字符串的宽度,类似于:

printf("%-15s", var);

这将创建一个15个字符的字段宽度。我已经做了很多谷歌搜索。我已经尝试使用COpaquepointer以及String(format:在不同的方式没有运气。任何建议将不胜感激。我可能会错过一些东西在谷歌搜索。

vc6uscn9

vc6uscn91#

可以使用withCString将字符串快速转换为字节数组(技术上是UnsafePointer<Int8>):

let str = "Hello world"
let formatted = str.withCString { String(format: "%-15s", $0) }

print("'\(formatted)'")
piwo6bdm

piwo6bdm2#

你还是自己来吧

let str0 = "alpha"
let length = 20
// right justify
var str20r = String(count: (length - str0.characters.count), repeatedValue: Character(" "))
str20r.appendContentsOf(str0)
// "               alpha"

// left justify
var str20l = str0
str20l.appendContentsOf(String(count: (length - str0.characters.count), repeatedValue: Character(" ")))
// "alpha               "

如果您需要“更一般”的内容

func formatString(str: String, fixLenght: Int, spacer: Character = Character(" "), justifyToTheRigth: Bool = false)->String {
    let c = str.characters.count
    let start = str.characters.startIndex
    let end = str.characters.endIndex
    var str = str
    if c > fixLenght {
        switch justifyToTheRigth {
        case true:
            let range = start.advancedBy(c - fixLenght)..<end
            return String(str.characters[range])
        case false:
            let range = start..<end.advancedBy(fixLenght - c)
            return String(str.characters[range])
        }
    } else {
        var extraSpace = String(count: fixLenght - c, repeatedValue: spacer)
        if justifyToTheRigth {
            extraSpace.appendContentsOf(str)
            return extraSpace
        } else {
            str.appendContentsOf(extraSpace)
            return str
        }
    }
}

let str = "ABCDEFGH"
let s0 = formatString(str, fixLenght: 3)
let s1 = formatString(str, fixLenght: 3, justifyToTheRigth: true)
let s2 = formatString(str, fixLenght: 10, spacer: Character("-"))
let s3 = formatString(str, fixLenght: 10, spacer: Character("-"), justifyToTheRigth: true)

print(s0)
print(s1)
print(s2)
print(s3)

其印刷了

ABC
FGH
ABCDEFGH--
--ABCDEFGH
drnojrws

drnojrws3#

问题在于Swift字符串的元素大小是可变的,所以“15个字符”的定义是模糊的。这对于简单的字符串来说是一个令人沮丧的来源--但在处理表情符号、地区标识符、连字等时,这使得语言更加精确。
您可以将Swift字符串转换为C字符串,并使用普通的格式化程序(参见Santosh的答案)。处理字符串的“Swift”方式是从Character集合的起始索引开始,前进N次。例如:

let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

let index = alphabet.characters.startIndex.advancedBy(14) // String.CharacterView.Index
let allChars = alphabet.characters.prefixThrough(index) // String.CharacterView

print(String(allChars)) // "ABCDEFGHIJKLMNO\n"

如果要强制填充,可以使用如下方法:

extension String {
    func formatted(characterCount characterCount:Int) -> String {
        if characterCount < characters.count {
            return String(characters.prefixThrough(characters.startIndex.advancedBy(characterCount - 1)))
        } else {
            return self + String(count: characterCount - characters.count, repeatedValue: " " as Character)
        }
    }
}

let abc = "ABC"
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

print("!\(abc.formatted(characterCount: 15))!")
// "!ABC            !\n"

print("!\(alphabet.formatted(characterCount: 15))!")
// "!ABCDEFGHIJKLMNOP!\n"
acruukt9

acruukt94#

你试过这个吗?

let string1 = "string1"
let string2 = "string2"
let formattedString = String(format: "%-15s - %s",
             COpaquePointer(string1.cStringUsingEncoding(NSUTF8StringEncoding)!),
             COpaquePointer(string2.cStringUsingEncoding(NSUTF8StringEncoding)!)
)

print(formattedString)
//string1         - string2
vhmi4jdf

vhmi4jdf5#

我们现在有很多有趣的答案。谢谢大家。我写了以下内容:

func formatLeftJustifiedWidthSpecifier(stringToChange: String, width: Int) -> String {

    var newString: String = stringToChange
    newString = newString.stringByPaddingToLength(width, withString: " ", startingAtIndex: 0)
    return newString
}
s6fujrry

s6fujrry6#

From one hand %@ is used to format String objects:

import Foundation

var str = "Hello"
print(String(format: "%@", str))

But it does not support the width modifier:

print(String(format: "%-15@", str))

Will still print unpadded text:

"Hello\n"

However there is a modifier %s that seems to work with CStrings:

var cstr = (str as NSString).utf8String //iOS10+ or .UTF8String otherwise

print(String(format: "%-15s", cstr!))

Output:

"Hello          \n"

One nice thing is that you can use the same format specification with NSLog:

NSLog("%-15s", cstr!)
yhxst69z

yhxst69z7#

在2016年6月29日,用“Code Different(代码不同)”(谢谢!)来补充上面的答案,并允许写类似于"hello".center(42); "world".alignLeft(42)的内容:

extension String {

    // note: symbol names match to nim std/strutils lib

    func align (_ boxsz: UInt) -> String {
        self.withCString { String(format: "%\(boxsz)s", $0) }
    }

    func alignLeft (_ boxsz: UInt) -> String {
        self.withCString { String(format: "%-\(boxsz)s", $0) }
    }

    func center (_ boxsz: UInt) -> String {
        let n = self.count
        guard boxsz > n else { return self }
        let padding = boxsz - UInt(n)
        let R = padding / 2
        guard R > 0 else { return " " + self }
        let L = (padding%2 == 0) ? R : (R+1)
        return " ".withCString { String(format: "%\(L)s\(self)%\(R)s", $0,$0) }
    }

}

相关问题