2014-01-14 216 views
0

如果字符串是“Testing for Group”,那么如何查找“rou”是否为子串。在输入字符串中查找字符串中的子串

使用方法该链接

substring with string

上只给完整的单词像串存在或不存在。

像上面的字符串中的“for”,但是我想找到“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]; 
} 
+1

请出示您的实际代码。 'rangeOfString:'不仅匹配完整的单词。 –

+0

@MatthiasBauch我的代码 – ViruMax

+0

对我来说看起来不错。你应该检查哪个字符串被实际比较,也许你的输入不是你所期望的。尝试用这个NSLog(@“string \”%@ \“不包含\”%@ \“”,[[[permArrayGroupName objectAtIndex:count] objectGroupName]大写字符串],[searchText大写字符串]);'看看哪些字符串被实际使用。 –

回答

1

以下工作:

NSString *[email protected]"Testing for Group"; 

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