Reputation: 1883
i have a string..
I am entering some characters from user using that string.
Now i want to check that in that string "a" is there or not?
can anyone tell me how to do this?
that "a" can be anywhere in the string.
Upvotes: 2
Views: 1332
Reputation: 23545
if ([myString rangeOfString:@"a"].location != NSNotFound) {
// "a" IS in myString
} else {
// "a" ISN'T in myString
}
if ([myString rangeOfString:@"a"].location != NSNotFound && [myString rangeOfString:@"b"].location != NSNotFound) {
// "a" AND "b" are BOTH in myString
} else if ([myString rangeOfString:@"a"].location != NSNotFound) {
// ONLY "a" is in myString
} else if ([myString rangeOfString:@"b"].location != NSNotFound) {
// ONLY "b" is in myString
} else {
// Neither "a" NOR "b" is in myString
}
Upvotes: 7
Reputation: 672
- (BOOL)isCharaterExist:(NSString *)sPhrase withSearchChar:(NSString *)sChar {
NSAssert(sPhrase != nil && sChar != nil, @"sPhrase and sChar should not be nil");
return ([sPhrase rangeOfString:sChar].length > NSNotFound);
}
Note: It's not tested in running environment, so typo mistakes can be possible!
Upvotes: 2