有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

转换ß。在java中使用toUpperCase()将cfg转换为大写

我正在尝试下面的代码

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

我得到的输出是SS.CFG,因为Unicode没有定义ß的大写版本,而我希望输出是ß.CFG

我有没有办法做到这一点


共 (5) 个答案

  1. # 1 楼答案

    Java实现只是遵循Unicode规范所说的。Unicode表示:

    # ================================================================================
    # 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
    

    参考:http://unicode.org/Public/UNIDATA/SpecialCasing.txt

    如果您想要实现一种不同于Unicode的大写转换形式,您需要自己指定并实现它


    (如果你想看到一群人对“大写字母ß”发火,请阅读这封电子邮件-http://unicode.org/mail-arch/unicode-ml/y2007-m05/0007.html

  2. # 2 楼答案

    {a1}的文档明确指出,将发生以下情况:

    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

  3. # 3 楼答案

    看起来Characeter.toUpperCase()忽略了这些规则,因此您可以使用它来实现所需的转换:

    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.

  4. # 4 楼答案

    “ß”字符相当于“ss”(例如,在德语中使用),这是在您的语言环境(您在应用程序中使用的语言环境)中定义的

    您可以尝试使用以下方法使用不同的区域设置进行一些实验:

    toUpperCase(Locale locale) 
    

    编辑:正如用户所说,此方法无效,一个可能的解决方法(不是很优雅)是:

        String s1 = new String ("auß.cfg").replace('ß', '\u9999');
        System.out.println (s1.toUpperCase(Locale.UK).replace('\u9999', 'ß'));
    
  5. # 5 楼答案

    试试^{}