0
0

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.

[Objective-C]文字列一覧を文字列の部分検索する

0
Last updated at Posted at 2020-01-31

「名前に検索文字列の一部でも入っていたら検索結果にだしてー」という内容。

元のコードで長ったらしー階段コードがあったので書き直す。こんな感じかなー。
Swiftならもっと簡単に書けるんだけどなー。

    
NSString* str = @"一二三四五";
NSArray* nameList = @[@"一ノ瀬",@"五十嵐",@"山田",@"二宮",@"佐藤",@"一二"];
NSMutableArray* results = @[].mutableCopy;

for (NSString* name in nameList){
    [str enumerateSubstringsInRange:NSMakeRange(0, str.length)
                            options:NSStringEnumerationByComposedCharacterSequences
                         usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) {
        if ([name containsString:substring] && ![results containsObject:name]) {
            [results addObject:name];
            *stop = YES;
        }
    }];
}

for (NSString* result in results){
    NSLog(@"result = %@",result);
}

結果

result = 一ノ瀬
result = 五十嵐
result = 二宮
result = 一二
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?