views:

51

answers:

3

I need to get, is a character already in the range. The character string is of type NSMuttableString. For example, I have a string of "52.648" and I need to know is a "." symbol is already in the string. How can I do it?

+1  A: 

The following expression is true if and only if myChar is within myString:

[myString rangeOfString: [NSString stringWithFormat: @"%c", myChar]
                options: NULL].location != NSNotFound
Jon Rodriguez
+6  A: 

You can use the rangeOfString message on the NSString:

NSRange rng = [string rangeOfString:@"."];
if (rng.location != NSNotFound)
    // "." is in the string at position rng.location
Cannonade
Just an addition. I used this and even with a heap of NSLogs confirming correct data NSNotFound didn't work as expected. The link below has a solution IF you have this problem.
Rudiger
@Rudiger Thanks man. I checked out the linked answer and up-voted it :)
Cannonade
A: 

Just as addition to the other answers which are correct. I've had issues using NSNotFound. Not sure if its a bug or not but using range.length > 0 works as intended. I posted about it here

Rudiger