通过 NSArray 搜索字符串 [英] Search through NSArray for string

查看:90
本文介绍了通过 NSArray 搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 NSArray 中搜索某个字符串.

I would like to search through my NSArray for a certain string.

示例:

NSArray 有对象:dog"、cat"、fat dog"、thing"、another thing"、heck here's another thing"

NSArray has the objects: "dog", "cat", "fat dog", "thing", "another thing", "heck here's another thing"

我想搜索单词another"并将结果放入一个数组中,并将另一个非结果放入另一个可以进一步过滤的数组中.

I want to search for the word "another" and put the results into one array, and have the other, non results, into another array that can be filtered further.

推荐答案

未测试所以可能有语法错误,但你会明白的.

Not tested so might have a syntax error, but you'll get the idea.

NSArray* inputArray = [NSArray arrayWithObjects:@"dog", @"cat", @"fat dog", @"thing", @"another thing", @"heck here's another thing", nil];

NSMutableArray* containsAnother = [NSMutableArray array];
NSMutableArray* doesntContainAnother = [NSMutableArray array];

for (NSString* item in inputArray)
{
  if ([item rangeOfString:@"another"].location != NSNotFound)
    [containsAnother addObject:item];
  else
    [doesntContainAnother addObject:item];
}

这篇关于通过 NSArray 搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆