问题描述
我想知道为什么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.
推荐答案
来自 Character#toUpperCase(int)
Javadoc,
From the Character#toUpperCase(int)
Javadoc,
所以,答案就是您的第二示例(String.toUpperCase
)
So, the answer is your second example (String.toUpperCase
)
这篇关于为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!