简体   繁体   中英

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?

can anyone tell me how to do this?

that "a" can be anywhere in the string.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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