在NSString中查找特定字符的位置

时间:2021-10-02 20:08:26

How can I find the location of a specific character in an NSString?

如何在NSString中找到特定字符的位置?

I have tried this but it returns an empty range (I want to find the location of the '?'):

我已经尝试过了,但是它返回一个空的范围(我想找到“?”的位置):

NSRange range = [@"This is a ? test to see if it works" rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"?"]];

2 个解决方案

#1


1  

You must doing something else wrong. Because this is absolutely correct and returns {10, 1}. At least on my mac it does ;-)

你必须做些别的错事。因为这是绝对正确的,返回{10,1}。至少在我的mac上是这样的;

#2


3  

maybe this will work?

也许这将工作吗?

NSString* s = @"hello?";
NSRange range = [s rangeOfString:@"?"];

#1


1  

You must doing something else wrong. Because this is absolutely correct and returns {10, 1}. At least on my mac it does ;-)

你必须做些别的错事。因为这是绝对正确的,返回{10,1}。至少在我的mac上是这样的;

#2


3  

maybe this will work?

也许这将工作吗?

NSString* s = @"hello?";
NSRange range = [s rangeOfString:@"?"];