使用输入字符串查找字符串中的子字符串

时间:2014-01-14 12:06:16

标签: iphone objective-c nsstring substring

如果string是“Testing for Group”,那么如何查找“rou”是否为子串。

使用此链接上的方法

substring with string

仅提供完整字,如子字符串是否存在。

喜欢上面的字符串中的“for”,但是我想找到“或”,“rou”就像字符串中存在的字符串一样。

怎么做?

提前致谢。

编辑:

我的代码

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    int count=0;
    arrayGroupName=[[NSMutableArray alloc]init];
    if ([searchText isEqualToString:@""])
    {
        arrayGroupName=permArrayGroupName;
    }
    else
    {
        while (count!=[permArrayGroupName count])
        {


            if ([[[[permArrayGroupName objectAtIndex:count] objectGroupName]capitalizedString] rangeOfString:[searchText capitalizedString]].location == NSNotFound )
            {
                NSLog(@"string does not contain bla");
            }
            else
            {
                NSLog(@"found");
                [arrayGroupName addObject:[permArrayGroupName objectAtIndex:count]];
            }
            /*if ([[[[permArrayGroupName objectAtIndex:count] objectGroupName]capitalizedString] hasPrefix:[searchText capitalizedString]])
            {
                [arrayGroupName addObject:[permArrayGroupName objectAtIndex:count]];
            }*/
            count++;
        }
    }
    [tableGroupList reloadData];
} 

1 个答案:

答案 0 :(得分:1)

以下作品:

NSString *string=@"Testing for Group";

if ([string rangeOfString:@"rou"].location == NSNotFound) {
    NSLog(@"Not found");
}
else {
    NSLog(@"Found");
}