为什么要使用NSString的uppercaseStringWithLocale来获取正确的大写字符串?

时间:2021-11-28 20:02:36

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.

我有一个非常简单的任务:从服务器获得UTF-8字符串作为字节数组,我需要从这个字符串中显示所有的符号。从这个字符串可以得到unicode表的任何符号。

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?

在我看来,它与我检查过的所有符号一起工作。但我不明白:为什么我们要用大写字母?当我们使用unicode时,每个符号在unicode表中都有惟一的位置,我们可以很容易地找到它是否具有大小写表示。如果我用大写字母来代替大写字母,会有什么麻烦呢?

2 个解决方案

#1


6  

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].

下面的方法根据指定的地区执行本地化的案例映射。传递nil表示规范映射。对于用户首选项locale设置,请指定+[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.

通常在某些地方,即使在字符集内,从小写到大写的映射也会发生变化。我不是世界上所有语言的专家,但编写这些方法的人是,所以我使用它们。

#2


20  

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.

大写化是依赖。例如,大写版本的“我”是“我”在英语中,但在土耳其“İ”。如果你总是使用英语规则,那么土耳其语单词的大写将会以错误告终。

#1


6  

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].

下面的方法根据指定的地区执行本地化的案例映射。传递nil表示规范映射。对于用户首选项locale设置,请指定+[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.

通常在某些地方,即使在字符集内,从小写到大写的映射也会发生变化。我不是世界上所有语言的专家,但编写这些方法的人是,所以我使用它们。

#2


20  

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.

大写化是依赖。例如,大写版本的“我”是“我”在英语中,但在土耳其“İ”。如果你总是使用英语规则,那么土耳其语单词的大写将会以错误告终。