views:

161

answers:

2

Have got an

NSString *str = @"12345.6789"

and want to find out if there is that "." character inside of it. I'm afraid that there are ugly char-encoding issues when I would just try to match an @"." against this? How would you do it to make sure it always finds a match if there is one?

I just need to know that there is a dot in there. Everything else doesn't matter.

+7  A: 

You can use rangeOfString: message to get the range where your "." is.

The prototype is:

- (NSRange)rangeOfString:(NSString *)aString

You can find more info about this message in: Mac Dev Center

There would be something like this:

NSRange range;  
range = [yourstring rangeOfString:@"."]; 
NSLog(@"Position:%d", range.location);

If you need to, there is another message ( rangeOfString:options: ) where you can add some options like "Case sensitive" and so on.

HyLian
+2  A: 

If [str rangeOfString:@"."] returns anything else than {NSNotFound, 0}, the search string was found in the receiver. There are no encoding issues as NSString takes care of encoding. However, there might be issues if your str is user-provided and could contain a different decimal separator (e.g., a comma). But then, if str really comes from the user, many other things could go wrong with that comparison anyway.

Ole Begemann