开发者

Position of a character NSString

开发者 https://www.devze.com 2023-01-25 00:18 出处:网络
I have string which contains either A ,B, C, or D开发者_C百科(exampleA123 or B235 or 2B35but not AB123)

I have string which contains either A ,B, C, or D开发者_C百科 (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??


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号