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];
} 
4
  • 1
    Please show you actual code. rangeOfString: does NOT only match complete words. Commented Jan 14, 2014 at 12:09
  • @MatthiasBauch my code Commented Jan 14, 2014 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. Commented Jan 14, 2014 at 12:20
  • Thanks I got answer, in my code I just removed capitalizedString, now its working fine. Commented Jan 14, 2014 at 12:26

1 Answer 1

1

The following works:

NSString *string=@"Testing for Group";

if ([string rangeOfString:@"rou"].location == NSNotFound) {
    NSLog(@"Not found");
}
else {
    NSLog(@"Found");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.