Search code examples
objective-cnsstringnsmutablestringnscharacterset

How to Compare Two Different Strings (string1 contains string2)


I am having Two Strings:string1 and string2. String1 contains only "ball", string2 contains "ball,fruit,doll".

Now i need to compare the string1 and string2, ball is in both the strings or not? and i need to remove the ball after comparing the strings. How to acheive this?


Solution

  • NSString *string1 = @"ball,fruit,doll";
    NSString *string2 = @"ball";
    if ([string1 rangeOfString:string2].location == NSNotFound) 
    {
        NSLog(@"string does not contain %@", string2);
    } 
    else 
    {
        NSLog(@"string contains %@", string2);
    }