为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数 [英] Why Java Character.toUpperCase/toLowerCase has no Locale parameter like String.toUpperCase/toLowerCase

查看:192
本文介绍了为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么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:

  1. 使用Character.toUpperCase

  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

  • 使用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/toLowerCaseString.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 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.

    所以,答案就是您的第二示例(String.toUpperCase)

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

    这篇关于为什么Java Character.toUpperCase/toLowerCase没有像String.toUpperCase/toLowerCase这样的Locale参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆