我正在建立一个项目,告诉我一段文本中的独特单词。
我有我的原始字符串scriptTextView,已将每个单词添加到数组scriptEachWordInArray
我现在想创建一个名为scriptUniqueWords的数组,该数组仅包含在scriptEachWordInArray中出现一次的单词(换句话说,是唯一的)
因此,我希望我的scriptUniqueWords数组等于= [“ Silent”,“ Holy”]。
我不想创建一个没有重复项的数组,而是创建一个仅具有第一次出现一次值的数组。
var scriptTextView = "Silent Night Holy Night"
var scriptEachWordInArray = ["Silent", "night", "Holy", "night"]
var scriptUniqueWords = [String]()
for i in 0..<scriptEachWordInArray.count {
if scriptTextView.components(separatedBy: "\(scriptEachWordInArray[i]) ").count == 1 {
scriptUniqueWords.append(scriptEachWordInArray[i])
print("Unique word \(scriptEachWordInArray[i])")}
}
Run Code Online (Sandbox Code Playgroud) I have this NS search expression. searchString passes in a String which I would like to search for in the baseString and highlight. However at the moment if I search for the word 'I' an 'i' in the word 'hide' for example appears highlighted.
I've seen that I can use \b to search for only whole words but I can't see where I add this into the expression. So that only whole words are …