Re: NSString character manipulation...
Re: NSString character manipulation...
- Subject: Re: NSString character manipulation...
- From: Greg Titus <email@hidden>
- Date: Mon, 1 Jul 2002 11:30:39 -0700
Hi Hsu,
On Monday, July 1, 2002, at 12:24 AM, Hsu wrote:
>
Hullo,
>
>
Is it safe to do something like:
...
>
Or am I going to have false positives because of unicode characters?
>
if ( (isalnum([myNSString characterAtIndex:0]))
This is not safe. If isalnum() was a real function, then the 2-byte
unichar will get cast to a 1-byte char as it is past into the function,
and you'll end up with false positives. However, isalnum() is a macro
which basically does a lookup in a table, using the character as an
index. So I'd expect you might even have your app crash if you pass in
too large of a value here.
You need to do something like this instead:
if ([[NSCharacterSet alphanumericCharacterSet]
characterIsMember:[myNSString characterAtIndex:0])
... which will work with all unicode characters. See the NSCharacterSet
documentation in Foundation.
>
|| ([myNSString characterAtIndex:0] == '.') ) {
This, on the other hand, will work just fine. The '.' will be implicitly
promoted to the right size for the comparison, and the Unicode standard
specifically made the low characters have exactly the same values as
ASCII so that comparisons like this are correct.
Hope this helps,
--Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.