简体   繁体   English

如何检查字符串中的某些字符?

[英]how to check for some character in the string?

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? 现在我想检查那个字符串“a”是否存在?

can anyone tell me how to do this? 谁能告诉我怎么做?

that "a" can be anywhere in the string. “a”可以在字符串中的任何位置。

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
}
- (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! 注意:它没有在运行环境中测试,因此可能出现错字错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM