On Sep 27, 2008, at 2:23 PM, David Niemeijer wrote:

Hi,

I have been trying to find this in the documentation and list archives but without success so far. What is the best way to count the number of characters in an NSString taking account of the fact that some characters may take up multiple 16 bit slots. Using "- (NSUInteger)length" is thus not the right way.

If I am reading you right, you are saying that -length will give you the wrong results because some characters in Unicode are represented by multibyte sequences. This is incorrect: -length will give you the number of Unicode characters in a string, not the number of bytes.

However, there are characters like "combining grave accent" (U+0300) that will usually not be displayed as a separate character, so there is a potential problem if you want to know how many characters will actually be displayed. The solution is to put the string into one of the composed Normalization Forms with either - precomposedStringWithCanonicalMapping (NFC) or - precomposedStringWithCompatibilityMapping (NFKC), depending on your needs. Then calling -length should give you the result you are looking for.

For information on Unicode Normalization Forms, see http://unicode.org/reports/tr15/ .

-Michael
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to