为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数 [英] Why Java Character.toUpperCase/toLowerCase has no Locale parameter like String.toUpperCase/toLowerCase
问题描述
我想知道为什么Character.toUpperCase/toLowerCase
没有像String.toUpperCase/toLowerCase
这样的Locale参数.
I am wondering that why Character.toUpperCase/toLowerCase
has no Locale parameter like String.toUpperCase/toLowerCase
.
我必须将任何语言的文本首字母大写.我有2个解决方案:
I have to first uppercase of a text that can be in Any language. I have 2 solutions:
-
使用
Character.toUpperCase
Use
Character.toUpperCase
String text = "stack overflow";
StringBuilder sb = new StringBuilder(text);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); // No Locale parameter here.
String out = sb.toString(); //Out: Stack overflow
使用String.toUpperCase
Use String.toUpperCase
Locale myLocale = new Locale(locateId);
String text = "stack overflow";
String text1 = text.substring(0,1).toUpperCase(myLocale );
String text2 = text.substring(1);
String out = text1 + text2; // Out: Stack overflow
对于我的语言环境.两种方法的结果相同.
For my Locale. Both way has the same result.
我的问题是:
-
因为文本可以是任何语言.我应该使用哪种方式?
Since the text can be in any language. Which way should I use?
为什么Character.toUpperCase/toLowerCase
没有Locale参数,因为Character.toUpperCase/toLowerCase
和String.toUpperCase/toLowerCase
之间没有太大区别,因为字符串是字符数组.
Why Character.toUpperCase/toLowerCase
has no Locale parameter because there is not much difference between Character.toUpperCase/toLowerCase
and String.toUpperCase/toLowerCase
because String is array of Characters.
推荐答案
From the Character#toUpperCase(int)
Javadoc,
通常,应使用
String.toUpperCase()
将字符映射为大写.String
案例映射方法比Character
案例映射方法具有多个优点.String
大小写映射方法可以执行区域设置敏感的映射,上下文敏感的映射和1:M字符映射,而Character
大小写映射方法则不能.
In general,
String.toUpperCase()
should be used to map characters to uppercase.String
case mapping methods have several benefits overCharacter
case mapping methods.String
case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas theCharacter
case mapping methods cannot.
所以,答案就是您的第二示例(String.toUpperCase
)
So, the answer is your second example (String.toUpperCase
)
这篇关于为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!