2010-11-15 5 views
1

J'ai chaîne qui contient soit A, B, C ou D (exemple A123 ou B235 ou 2B35 mais pas AB123)position d'un caractère NSString

Je veux trouver l'indice A, B, C ou D

en C# on écrit comme

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

Comment écrire à Objective-C ??

Répondre

11

Vous pouvez utiliser -rangeOfCharacterFromSet::

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

if (range.location == NSNotFound) { 
    // ... oops 
} else { 
    // range.location is the index 
} 
Questions connexes