我有一个字符串..
我正在使用该字符串从用户输入一些字符。
现在我想检查那个字符串“a”是否存在?
谁能告诉我怎么做?
“a”可以在字符串中的任何位置。
答案 0 :(得分:7)
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
}
答案 1 :(得分:2)
- (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);
}
注意:它没有在运行环境中测试,因此可能出现错字错误!