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