0

If string is "Testing for Group" then how to find if "rou" is substring.

By using method on this link

substring with string

only gives complete word like substring is present or not.

like "for" in above string , but I want to find "or" , "rou" like strings present in string or not.

How to do this?

Thanks in advance.

EDIT:

my code

- (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];
} 
Community
  • 1
  • 1
ViruMax
  • 1,146
  • 3
  • 14
  • 40
  • 1
    Please show you actual code. `rangeOfString:` does NOT only match complete words. – Matthias Bauch Jan 14 '14 at 12:09
  • @MatthiasBauch my code – ViruMax Jan 14 '14 at 12:14
  • Looks okay to me. You should inspect which string are actually compared, maybe your inputs are not what you expect. Try to replace your "not found" NSLog with this `NSLog(@"string \"%@\" does not contain \"%@\"", [[[permArrayGroupName objectAtIndex:count] objectGroupName]capitalizedString], [searchText capitalizedString]);` to see which strings are actually used. – Matthias Bauch Jan 14 '14 at 12:20
  • Thanks I got answer, in my code I just removed capitalizedString, now its working fine. – ViruMax Jan 14 '14 at 12:26

1 Answers1

1

The following works:

NSString *string=@"Testing for Group";

if ([string rangeOfString:@"rou"].location == NSNotFound) {
    NSLog(@"Not found");
}
else {
    NSLog(@"Found");
}
Anoop Vaidya
  • 45,475
  • 15
  • 105
  • 134