我正在使用 Swift。 我正在尝试将一个句子转换为一个字符串数组。我使用 map 将句点和逗号与单词分开,如下所示:
extension String {
func convertSentenceToArray()-> [String] {
var sentence = String(self)
sentence.index(of: ".").map {
sentence.remove( at: $0)
sentence.insert(".", at: $0)
sentence.insert(" ", at: $0)
}
sentence.index(of: ",").map {
sentence.remove( at: $0)
sentence.insert(",", at: $0)
sentence.insert(" ", at: $0)
}
return sentence.components(separatedBy: " ")
}
}
let thisSentenceString = "I am trying to create an array from a sentence. But I don't understand, Why isn't the last fullstop removed, from the last word."
let thisSentenceArray = thisSentenceString.convertSentenceToArray()
print(thisSentenceArray)
结果:
["I", "am", "trying", "to", "create", "an", "array", "from", "a", "sentence", ".", "But", "I", "don\'t", "understand", ",", "Why", "isn\'t", "the", "last", "fullstop", "removed,", "from", "the", "last", "word."]
除最后一个外,所有句点和逗号都按我预期的方式处理。
我不明白为什么最后一个句号仍然存在。虽然我可以找到解决此问题的方法,但我想了解我所采用的方法有什么问题。
最佳答案
首先解释一下你的代码做了什么:
sentence
.index(of: ".") // find the first index of the dot character
.map { // Optional.map, if the index exists, do the following
sentence.remove( at: $0) // remove dot
sentence.insert(".", at: $0) // insert dot again
sentence.insert(" ", at: $0) // insert space
}
或重写:
if let firstDotIndex = sentence.index(of: ".") {
sentence.insert(" ", at: firstDotIndex)
}
这意味着只找到并替换第一个点字符。
要正确执行此算法,您需要:
// helper method checking punctuation to avoid code duplication
let isPunctuation: (Character) -> Bool = {
return [".", ","].contains($0)
}
// initial range, we want to check the entire string
var range = sentence.startIndex...
// iterate while some punctuation exists
while let punctuationIndex = sentence[range].index(where: isPunctuation) {
// insert the separator
sentence.insert(" ", at: punctuationIndex)
// search next punctuation only from the last replacement
range = sentence.index(after: punctuationIndex)...
}
不过,其实已经有String
替换的方法了:
sentence = sentence.replacingOccurrences(of: ".", with: " .")
或者更简单,用一个正则表达式一次性覆盖所有标点符号:
return self
.replacingOccurrences(of: "[,.]", with: " $0", options: .regularExpression)
.components(separatedBy: " ")
关于arrays - 为什么不使用 .map 从 stringArray 中删除最后一个句号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467612/