Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we should use uppercaseStringWithLocale of NSString to get correct uppercase string?

I have a very simple task: from server I get UTF-8 string as byte array and I need to show all symbols from this string in upper case. From this string you can get really any symbol of unicode table.

I know how to do it in a line of code:

NSString* upperStr = [[NSString stringWithCString:utf8str encoding:NSUTF8StringEncoding] uppercaseString];

And seems to me it works with all symbols which I have checked. But I don't understand: why we need method uppercaseStringWithLocale? When we work with unicode each symbol has unique place in unicode table and we can easily find does it have upper/lower case representation. What trouble I might have if I use uppercaseString instead uppercaseStringWithLocale?

like image 934
John Green Avatar asked Jan 08 '14 12:01

John Green


2 Answers

Uppercasing is locale-dependent. For example, the uppercase version of "i" is "I" in English, but "İ" in Turkish. If you always apply English rules, uppercasing of Turkish words will end up wrong.

like image 167
Philipp Avatar answered Oct 04 '22 22:10

Philipp


The docs say:

The following methods perform localized case mappings based on the locale specified. Passing nil indicates the canonical mapping. For the user preference locale setting, specify +[NSLocale currentLocale].

Assumedly in some locales the mapping from lowercase to uppercase changes even within a character set. I'm not an expert in every language around the globe, but the people who wrote these methods are, so I use 'em.

like image 38
Wil Shipley Avatar answered Oct 04 '22 23:10

Wil Shipley