我有包含 A 、B、C 或 D 的字符串(例如 A123 或 B235 或 2B35,但不包含 AB123)
我想找到A、B、C或D的索引
在 C# 中我们写为
String s = "123B";
index = s.IndexOfAny(new char[] = {A,B,C,D});
如何用 Objective-C 编写?
最佳答案
您可以使用-rangeOfCharacterFromSet:
:
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"ABCD"];
NSRange range = [string rangeOfCharacterFromSet:charSet];
if (range.location == NSNotFound) {
// ... oops
} else {
// range.location is the index
}
关于objective-c - NSString 字符的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4188156/