converting ß.cfg to upper case using toUpperCase() in java

前端 未结 5 1312
心在旅途
心在旅途 2021-01-18 05:47

I am trying following code

String s1 = \"ß.cfg\";
System.out.println (s.toUpperCase());

output I am getting is SS.CFG since U

相关标签:
5条回答
  • 2021-01-18 06:22

    The Java implementation is simply following what the Unicode specification says. And Unicode says this:

    # ================================================================================
    # Unconditional mappings
    # ================================================================================
    
    # The German es-zed is special--the normal mapping is to SS.
    # Note: the titlecase should never occur in practice. It is equal to titlecase(uppercase(<es-zed>))
    
    00DF; 00DF; 0053 0073; 0053 0053; # LATIN SMALL LETTER SHARP S
    

    Reference: http://unicode.org/Public/UNIDATA/SpecialCasing.txt

    If you want to implement a form of uppercase conversion that is different to Unicode, you'll need to specify and implement it yourself.


    (If you want to see a bunch of people getting hot under the collar about "uppercase ß", read this email thread - http://unicode.org/mail-arch/unicode-ml/y2007-m05/0007.html )

    0 讨论(0)
  • 2021-01-18 06:28

    "ß" character is equivalent to "ss" (used in German, for example), and this is defined so in your Locale (the Locale you are using in your app).

    You can try to do some experiment with a different Locale using method:

    toUpperCase(Locale locale) 
    

    Edit: As the user said, this method is not valid, a possible workaroud (not very elegant) is:

        String s1 = new String ("auß.cfg").replace('ß', '\u9999');
        System.out.println (s1.toUpperCase(Locale.UK).replace('\u9999', 'ß'));
    
    0 讨论(0)
  • 2021-01-18 06:30

    The documentation for toUpperCase( Locale ) explicitly states that this is what will happen:

    Since case mappings are not always 1:1 char mappings, the resulting String may be a different length than the original String.

    small letter sharp s -> two letters: SS

    0 讨论(0)
  • 2021-01-18 06:36

    It looks like Characeter.toUpperCase() ignores these rules, so that you can use it to implement the desired conversion:

    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.

    0 讨论(0)
  • 2021-01-18 06:39

    Try java.lang.String.toUpperCase(java.util.Locale).

    0 讨论(0)
提交回复
热议问题