func foundSubString(str:String,array:[String]) -> Bool {
var count = 0
repeat {
print("count : \(count)")
if str.lowercaseString.rangeOfString(array[count].lowercaseString) != nil {
print("founded")
return true
}
count += 1
} while count < array.count
return false
}
使用:
let str1 = "During the winter holiday I'll go skiing."
let str2 = "Do knock on the door or chime the bell"
let fixedSearchStrings = ["ring the", "chime the bell", "knock on the door", "knock on the window"]
let exist: Bool = foundSubString(str2,array: fixedSearchStrings)
結果:
如果你想了解你的更多細節,例如,如果你找到一個窩第三,你需要知道這是什麼字,他的位置是:
func foundSubString2(str:String,array:[String]) -> (Bool,[(String,Int)]) {
var count: Int = 0
var matched = [(String,Int)]()
repeat {
if str.lowercaseString.rangeOfString(array[count].lowercaseString) != nil {
matched.append((array[count],count))
}
count += 1
} while count < array.count
if matched.count>0 {
return (true,matched)
}
return (false,[("",0)])
}
使用:
let str1 = "During the winter holiday I'll go skiing."
let str2 = "Do knock on the door or chime the bell"
let fixedSearchStrings = ["ring the", "chime the bell", "knock on the door", "knock on the window", "knock on the door"]
let (exist,matched) = foundSubString2(str2,array: fixedSearchStrings)
if exist { print (matched) }
結果:
每個字符串轉換爲正則表達式,使前/後只能是空格和標點符號。 – Sulthan