1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

配列の中を検索 Swift

Last updated at Posted at 2016-08-03

#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]
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?