字符NSString的位置 [英] Position of a character NSString
本文介绍了字符NSString的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,其中包含A,B,C或D(例如A123或B235或2B35,但不是AB123)
I have string which contains either A ,B, C, or D (example A123 or B235 or 2B35 but not AB123)
我想找到索引A,B,C或D
I want to find the index of A,B,C or D
在C#中我们写为
String s = "123B";
index = s.IndexOfAny(new char[] = {A,B,C,D});
如何在Objective-C中编写
How to write in Objective-C??
推荐答案
您可以使用 -rangeOfCharacterFromSet:
:
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
}
这篇关于字符NSString的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文