Pregunta

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?

¿Fue útil?

Solución

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);
}

Otros consejos

To compare strings you can use isEqualToString

To find out whether one string contains another one you can use rangeOfString

Take a look at the documentation of NSString, the problem is really trivial: http://developer.apple.com/library/ios/#documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top