Egil
Egil

Reputation: 73

Find location of specific character in NSString

How can I find the location of a specific character in an 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:@"?"]];

Upvotes: 3

Views: 4842

Answers (2)

V1ru8
V1ru8

Reputation: 6147

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

Upvotes: 1

jmont
jmont

Reputation: 423

maybe this will work?

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

Upvotes: 3

Related Questions