1

I have string which contains either A ,B, C, or D (example A123 or B235 or 2B35 but not AB123)

I want to find the index of A,B,C or D

In C # we write as

String s = "123B";    
index = s.IndexOfAny(new char[] = {A,B,C,D});

How to write in Objective-C??

Georg Fritzsche
  • 95,426
  • 26
  • 188
  • 233
Ravit Teja
  • 301
  • 6
  • 12

1 Answers1

11

You can use -rangeOfCharacterFromSet::

NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCD"];
NSRange range = [string rangeOfCharacterFromSet:charSet];

if (range.location == NSNotFound) {
    // ... oops
} else {
    // range.location is the index
}
FreeAsInBeer
  • 12,891
  • 5
  • 48
  • 80
Georg Fritzsche
  • 95,426
  • 26
  • 188
  • 233