如何在保留分隔字符串的同时将字符串拆分为数组?
您可以这样做来拆分字符串,但不会保留分隔符的子字符串:
“我的名字” .components(separatedBy:“-”)-> [“我的”,“名字”]
这是我要实现的目标:
例1:“我的名字” .someSplitFunc(separatedBy:“-”)-> [“我的”,“名字”]
例2:“这是一个句子”。someSplitFunc(separatedBy:“-”)-> [“ This-”, “是-”,“ a-”,“句子”]
答案 0 :(得分:2)
您可以尝试以下方法:
var str = "This-is-a-sentence"
var separateList = str.components(separatedBy: "-")
for (index, _) in separateList.enumerated() {
if index < separateList.count - 1 {
separateList[index].append("-")
}
}
print(separateList)
答案 1 :(得分:0)
AFAIK没有本机Swift方法可以完成您想要的操作。您将需要实现扩展Collection
的自己的split方法。像这样:
extension Collection {
func splitAndKeep(separator: Element, maxSplits: Int = .max, omittingEmptySubsequences: Bool = true) -> [SubSequence] where Element: Equatable {
splitAndKeep(maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences) { $0 == separator }
}
func splitAndKeep(maxSplits: Int = .max, omittingEmptySubsequences: Bool = true, whereSeparator isSeparator: (Element) throws -> Bool) rethrows -> [SubSequence] {
guard !isEmpty else { return [] }
let positions = try indices.filter { try isSeparator(self[$0]) }
var subSequences: [SubSequence] = []
var start = startIndex
for position in positions {
let subsequence = self[start...position]
if !subsequence.isEmpty {
subSequences.append(subsequence)
if subSequences.count == maxSplits {
return subSequences
}
}
start = index(after: position)
}
let tail = self[start...]
if subSequences.count < maxSplits, !omittingEmptySubsequences || !tail.isEmpty {
subSequences += CollectionOfOne(tail)
}
return subSequences
}
}
let string = "This-is-a-sentence"
string.splitAndKeep(separator: "-") // ["This-", "is-", "a-", "sentence"]
let str2 = "This is a sentence, and this is another one. Hello World!"
let subsequences = str2.splitAndKeep(whereSeparator: \.isPunctuation) // ["This is a sentence,", " and this is another one.", " Hello World!"]
let firstSentence = str2.splitAndKeep(maxSplits: 1, whereSeparator: \.isPunctuation) // ["This is a sentence,"]
答案 2 :(得分:0)
只需在下面使用此扩展名即可。
extension String {
func componentSeparate(by: String) -> [String] {
var out: [String] = [String]()
var storeStr = ""
for str in Array(self) {
if by.contains(str) {
out.append(storeStr)
storeStr.removeAll()
continue
}
storeStr.append(str)
}
return out
}
func componentSeparated1(by: String) -> [String] {
var separateList: [String] = componentSeparate(by: by).map { "\($0)\(by)" }
separateList[separateList.count-1].removeLast(by.count)
return separateList
}
func componentSeparated2(by: String) -> [String] {
if self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return [] }
var separateList: [String] = componentSeparate(by: by).map { "\($0)\(by)" }
separateList[separateList.count-1].removeLast(by.count)
return separateList
}
func componentSeparated3(by: String) -> [String] {
if self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return [] }
var separateList: [String] = componentSeparate(by: by).map {"\($0)\(by)"}.filter {$0 != "-"}
separateList[separateList.count-1].removeLast(by.count)
return separateList
}
func componentSeparated4(by: String) -> [String] {
if self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { return [] }
let separateList: [String] = componentSeparate(by: by).map {"\($0)\(by)"}.filter {$0 != "-"}
return separateList
}
}
print("This-is-a-sentence-".componentSeparated1(by: "-"))
print("".componentSeparated2(by: "-"))
print("This-is-a-sentence-".componentSeparated3(by: "-"))
print("This-is-a-sentence-".componentSeparated4(by: "-"))
输出:
["This-", "is-", "a-", "sentence-", ""]
[]
["This-", "is-", "a-", "sentence"]
["This-", "is-", "a-", "sentence-"]