【问题标题】:Finding substring in string with input string使用输入字符串在字符串中查找子字符串
【发布时间】:2014-01-14 12:06:15
【问题描述】:

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

通过使用此链接上的方法

substring with string

只给出完整的单词,比如子字符串是否存在。

like "for" in above string ,但我想找到 "or" , "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];
} 

【问题讨论】:

  • 请显示实际代码。 rangeOfString: 不仅匹配完整的单词。
  • @MatthiasBauch 我的代码
  • 我觉得没问题。您应该检查实际比较了哪个字符串,也许您的输入不是您所期望的。尝试用 NSLog(@"string \"%@\" does not contain \"%@\"", [[[permArrayGroupName objectAtIndex:count] objectGroupName]capitalizedString], [searchText capitalizedString]); 替换您的“未找到”NSLog,以查看实际使用了哪些字符串。
  • 谢谢我得到答案,在我的代码中我刚刚删除了大写字符串,现在它工作正常了。

标签: iphone objective-c nsstring substring


【解决方案1】:

以下作品:

NSString *string=@"Testing for Group";

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 2021-08-13
    • 2015-10-28
    • 2017-12-04
    相关资源
    最近更新 更多