例如我有一個字符串文本,例如:「我必須去廚房」 如果我使用'av'短語搜索此文本,我想要一種方式使我返回整個字'擁有' 我怎麼能在swift中做到這一點從字符串中提取整個單詞在Swift中
1
A
回答
1
將你的字符串分割成數組,空格爲char(「」),並返回包含'av'字符串的組件。
0
let words = stringYouWantToSearchIn.componentsSeparatedByString(" ")
for word in words
{
var range = word.rangeOfString(lastWord)
if (range != nil)
{
//you got what do you want in 'word variable'
break
}
}
3
有與swift.You filter
非常好的解決方案可以使用的String
rangeOfString
法filter
獲得有「AV」
var s = "I have to go to the kitchen"
//will return "have"
let abc:[String] = s.componentsSeparatedByString(" ").filter({ $0.rangeOfString("av", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) != nil })
只過濾字符串
相關問題
- 1. 從字符串中提取單詞?
- 2. jQuery從字符串中提取單詞
- 3. Ruby:從字符串中提取單詞
- 4. 從一個單詞字符串中提取一個數字
- 5. 如何在Swift字符串中僅搜索整個單詞
- 6. 從字符串中提取單個單詞C++
- 7. 從unix中的字符串中提取一個單詞
- 8. 在Excel中從分隔字符串中提取單詞?
- 9. 從我的字符串中提取最後兩個單詞
- 10. 如何從一個拆分字符串中提取單詞
- 11. 從單詞圖像中提取字符
- 12. C從兩個單詞之間的字符串中提取單詞
- 13. 在swift中搜索一個字符串中的多個單詞
- 14. JS:從字符串中提取數字,即在特定單詞之前提取
- 15. 從句子字符串中提取每個單詞的第一個字符
- 16. 如何從Postgres中的字符串中提取特定單詞
- 17. 如何從python中的字符串中提取單詞?
- 18. 從Perl中的字符串中提取n-grams作爲單詞
- 19. 如何從ruby中的字符串中提取單詞
- 20. Python - 從字符串中提取數字/單詞時速度慢
- 21. 從字符串中提取以字母開頭的單詞
- 22. 從字符串中提取整數
- 23. 從字符串中提取整數值
- 24. 從JSON字符串中提取整數?
- 25. 從字符串中提取整數
- 26. 從字符串中提取整數
- 27. 在c中的兩個單詞之間提取字符串#
- 28. 如何從域名字符串中提取單詞
- 29. Ruby:如何從字符串中提取單詞
- 30. 使用preg_match_all從字符串中提取單詞