开发者

Position of a character NSString

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜