我正在使用以下代码从字符串中删除停用字词但最终修剪其他字样,我只想删除特定字词来自字符串而不修剪其他单词。
import Foundation
var sentence = "God have created the creatures at his best"
let wordToRemove = "at"
if let range = sentence.range(of: wordToRemove) {
sentence.removeSubrange(range)
}
print(sentence) // God have creed the creures his best
答案 0 :(得分:1)
首先编写Build-In类String
的扩展extension String {
func contains(word : String) -> Range<String.Index>?
{
return self.range(of: "\\b\(word)\\b", options: .regularExpression)
}
}
然后编写以下代码以删除句子中的特定单词
var sentence = "God have created the creatures at his best"
let wordToRemove = "at"
if let range = sentence.contains(word: wordToRemove) {
sentence.removeSubrange(range)
print(sentence)
}
//Output : God have created the creatures his best
答案 1 :(得分:1)
根据我对停用词的理解,它们可以出现在句子中的任何一点,包括作为第一个或最后一个词。因此,解决方案应支持在句子中的任何位置删除它们。
import Foundation
var sentence = "at God have created the creatures at his best at"
let wordsToRemove = ["at", "his"]
let words = sentence.components(separatedBy: " ")
sentence = words.filter({ wordsToRemove.contains($0) == false }).joined(separator:" ")
// sentence is now "God have created the creatures best"
答案 2 :(得分:0)
import Foundation
var sentence = "God have created the creatures at at at at his best"
let words = [" at ", " his "]
var index = 0
while index < words.count {
let word = words[index]
if let range = sentence.range(of: word) {
sentence = sentence.replacingOccurrences(of: word, with: " ", options: [], range: range)
} else {
index += 1
}
}
print(sentence)
上帝创造了最好的生物
答案 3 :(得分:0)
.closeme {
-webkit-animation-name: closeanimaton;
-webkit-animation-duration: 3s;
-webkit-animation-iteration-count: 1;
-webkit-animation-timing-function: ease-in-out;
-webkit-animation-delay: -1.5s;
-webkit-animation-direction: alternate;
-webkit-transform-origin: bottom;
animation-name: closeanimaton;
animation-duration: 3s;
animation-iteration-count: 1;
animation-timing-function: ease-in-out;
animation-delay: -1.5s;
animation-direction: alternate;
animation-fill-mode: forwards;
transform-origin: bottom;
-moz-animation: none;
}
@-webkit-keyframes closeanimaton {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(35deg); }
}
@keyframes closeanimaton {
0% { transform: rotate(0deg); }
100% { transform: rotate(35deg); }
}
你可以尝试这种方式。