Edited at

配列の中を検索 Swift

More than 3 years have passed since last update.


Swift2.2

検索でひっかかった要素を取り出す。

var stringArray = ["this is a testString, please add your sentence.","I like Apples", "Do you like apples?","今日はいい天気ですね", "はい、いい 天気です","私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

var searchWord = "雷 Apple 天気"
var searchArray = [String]()

//OR
searchArray = stringArray.filter{
for i in searchWord.componentsSeparatedByString(" "){
if $0.localizedCaseInsensitiveContainsString(i){
return true
}
}
return false
}
print(searchArray)//["I like Apples", "Do you like apples?", "今日はいい天気ですね", "はい、いい 天気です", "私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

//AND
searchArray = stringArray.filter{
for i in searchWord.componentsSeparatedByString(" "){
if !$0.localizedCaseInsensitiveContainsString(i){
return false
}
}
return true
}
print(searchArray)//["Apple社の天気は雷", "Apple社の天気は雷?"]

//一致
searchWord = "like Apple"
searchArray = stringArray.filter{$0.localizedCaseInsensitiveContainsString(searchWord)}
print(searchArray)//["I like Apples", "Do you like apples?"]

検索でひっかかった要素の番号を取得

var stringArray = ["this is a testString, please add your sentence.","I like Apples", "Do you like apples?","今日はいい天気ですね", "はい、いい 天気です","私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

var searchWord = "雷 Apple 天気"
var searchArray = [Int]()

//OR
searchArray = stringArray.enumerate().filter{
for i in searchWord.componentsSeparatedByString(" "){
if $1.localizedCaseInsensitiveContainsString(i){
return true
}
}
return false
}.map{$0.0}
print(searchArray)//[1, 2, 3, 4, 5, 6, 7]

//AND
searchArray = stringArray.enumerate().filter{
for i in searchWord.componentsSeparatedByString(" "){
if !$1.localizedCaseInsensitiveContainsString(i){
return false
}
}
return true
}.map{$0.0}
print(searchArray)//[6, 7]

//一致
searchWord = "like Apple"
searchArray = stringArray.enumerate().filter{$1.localizedCaseInsensitiveContainsString(searchWord)}.map{$0.0}
print(searchArray)//[1, 2]


Swift3

検索でひっかかった要素を取り出す。

var stringArray = ["this is a testString, please add your sentence.","I like Apples", "Do you like apples?","今日はいい天気ですね", "はい、いい 天気です","私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

var searchWord = "雷 Apple 天気"
var searchArray = [String]()

//OR
searchArray = stringArray.filter{
for i in searchWord.components(separatedBy:" "){
if $0.localizedCaseInsensitiveContains(i){
return true
}
}
return false
}
print(searchArray)//["I like Apples", "Do you like apples?", "今日はいい天気ですね", "はい、いい 天気です", "私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

//AND
searchArray = stringArray.filter{
for i in searchWord.components(separatedBy: " "){
if !$0.localizedCaseInsensitiveContains(i){
return false
}
}
return true
}
print(searchArray)//["Apple社の天気は雷", "Apple社の天気は雷?"]

//一致
searchWord = "like Apple"
searchArray = stringArray.filter{$0.localizedCaseInsensitiveContains(searchWord)}
print(searchArray)//["I like Apples", "Do you like apples?"]

検索でひっかかった要素の番号を取得

var stringArray = ["this is a testString, please add your sentence.","I like Apples", "Do you like apples?","今日はいい天気ですね", "はい、いい 天気です","私はappleが好きです", "Apple社の天気は雷", "Apple社の天気は雷?"]

var searchWord = "雷 Apple 天気"
var searchArray = [Int]()

//OR
searchArray = stringArray.enumerated().filter{
for i in searchWord.components(separatedBy: " "){
if $1.localizedCaseInsensitiveContains(i){
return true
}
}
return false
}.map{$0.0}
print(searchArray)//[1, 2, 3, 4, 5, 6, 7]

//AND
searchArray = stringArray.enumerated().filter{
for i in searchWord.components(separatedBy: " "){
if !$1.localizedCaseInsensitiveContains(i){
return false
}
}
return true
}.map{$0.0}
print(searchArray)//[6, 7]

//一致
searchWord = "like Apple"
searchArray = stringArray.enumerated().filter{$1.localizedCaseInsensitiveContains(searchWord)}.map{$0.0}
print(searchArray)//[1, 2]