Why Java Character.toUpperCase/toLowerCase has no Locale parameter like String.toUpperCase/toLowerCase

三世轮回 提交于 2019-12-06 20:33:15

问题


I am wondering that why Character.toUpperCase/toLowerCase has no Locale parameter like String.toUpperCase/toLowerCase.

I have to first uppercase of a text that can be in Any language. I have 2 solutions:

  1. 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
    
  2. 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.

My question is:

  • Since the text can be in any language. Which way should I use?

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


回答1:


From the Character#toUpperCase(int) Javadoc,

In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot.

So, the answer is your second example (String.toUpperCase)




回答2:


As the Javadoc says:

In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot.

So use String.toUppercase()




回答3:


If the question is "which method should I use", then this question is a duplicate of String conversion to Title Case and the correct answer is EITHER

  • Apache Commons Lang StringUtils.captitalize() - Javadoc at http://commons.apache.org/proper/commons-lang/javadocs/api-2.5/org/apache/commons/lang/StringUtils.html
  • Apache Commons Lang WordUtils.capitalize() - Javadoc at http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html. This capitalizes each individual word.

If the question is "why doesn't Character have locale-sensitive case-changing methods", then the only way you're likely to get an answer is to consult one of the designers of the Java language. It's unlikely that the Stack Overflow community will be able to give you the answer you want.



来源:https://stackoverflow.com/questions/26515060/why-java-character-touppercase-tolowercase-has-no-locale-parameter-like-string-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!