如何在每个N字符中快速添加分隔符?
我有一个包含二进制数字的string。 如何将它分成几对数字?
假设string是:
let x = "11231245"
我想在每个2个字符之后添加一个分隔符,例如“:”(即冒号)。
我想输出是:
"11:23:12:45"
我怎么能在Swift中做到这一点?
你可以使用stride(from:, to:, by:)
迭代你的string中的每2个字符,然后使用join(with: separator)
再次连接你的string。
斯威夫特4
extension String { var pairs: [String] { var result: [String] = [] let characters = Array(self) stride(from: 0, to: count, by: 2).forEach { result.append(String(characters[$0..<min($0+2, count)])) } return result } mutating func insert(separator: String, every n: Int) { self = inserting(separator: separator, every: n) } func inserting(separator: String, every n: Int) -> String { var result: String = "" let characters = Array(self) stride(from: 0, to: count, by: n).forEach { result += String(characters[$0..<min($0+n, count)]) if $0+n < count { result += separator } } return result } }
Swift 3
extension String { var pairs: [String] { var result: [String] = [] let characters = Array(self.characters) stride(from: 0, to: characters.count, by: 2).forEach { result.append(String(characters[$0..<min($0+2, characters.count)])) } return result } mutating func insert(separator: String, every n: Int) { self = inserting(separator: separator, every: n) } func inserting(separator: String, every n: Int) -> String { var result: String = "" let characters = Array(self.characters) stride(from: 0, to: characters.count, by: n).forEach { result += String(characters[$0..<min($0+n, characters.count)]) if $0+n < characters.count { result += separator } } return result } }
testing
let str1 = "112312451" let finalString1 = str1.pairs.joined(separator: ":") // "11:23:12:45:1" print(str1) // "112312451\n" let finalString2 = str1.inserting(separator: ":", every: 2) // "11:23:12:45:1" print(str1) // "112312451\n" var str2 = "112312451" str2.insert(separator: ":", every: 2) print(str2) // "11:23:12:45:1\n"
我对这个代码的尝试是:
func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String { var output = "" intoString.characters.enumerate().forEach { index, c in if index % afterEveryXChars == 0 && index > 0 { output += seperator } output.append(c) } return output } insert(":", afterEveryXChars: 2, intoString: "11231245")
哪个输出
11:23:12:45
let y = String( x.characters.enumerate().map() { $0.index % 2 == 0 ? [$0.element] : [$0.element, ":"] }.flatten() )
简单而简单,如果你想要添加一个或两个
extension String { func separate(every: Int, with separator: String) -> String { return String(stride(from: 0, to: Array(self).count, by: every).map { Array(Array(self)[$0..<min($0 + every, Array(self).count)]) }.joined(separator: separator)) } }
let a = "separatemepleaseandthankyou".separate(every: 4, with: " ")
a
是
独立的mepl放松和你hank
我的代码在迅速4
let x = "11231245" var newText = String() for (index, character) in x.enumerated() { if index != 0 && index % 2 == 0 { newText.append(":") } newText.append(String(character)) } print(newText)
产出11:23:12:45
我会去这个紧凑的解决scheme(在Swift 4中):
let r = String(s.enumerated().map { $0 > 0 && $0 % 2 == 0 ? [":", $1] : [$1]}.joined())
你可以做一个扩展和参数化的步幅和分隔符,以便你可以使用它为你想要的每个值(在我的情况下,我用它来转储32位空间操作的hex数据):
extension String { func separate(every stride: Int = 4, with separator: Character = " ") { return String(enumerated().map { $0 > 0 && $0 % stride == 0 ? [separator, $1] : [$1]}.joined()) } }
在你的情况下,这给出了以下结果:
let x = "11231245" print (x.separate(every:2, with: ":") $ 11:23:12:45