← 中文资源

简体中文版本

Locale 敏感的大小写转换

土耳其语区域把 i 大写成带点的 İ、I 小写成 ı;协议名等固定词必须用 Locale.ROOT,不带 Locale 的调用跟随默认区域,换台机器结果就会变。

阅读英文原文

实现说明

整个示例围绕类 CaseRules和函数 main组织。

使用要点

  • 演示输出是带标签的(root:、tr:、tr lower:),每个操作都会留下自己的一行标注。

常见问题

运行「Locale 敏感的大小写转换」会打印什么?

整个文件跑完,输出行以 root:、tr:、tr lower: 这类前缀开头。

核心语句

Locale turkish = Locale.of("tr", "TR");

代码示例

import java.util.Locale;

public class CaseRules {

    public static void main(String[] args) {
        Locale turkish = Locale.of("tr", "TR");

        // the default rule: i uppercases to I
        System.out.println("root: " + "i".toUpperCase(Locale.ROOT));

        // the Turkish rule: i becomes the dotted capital İ, a different character
        System.out.println("tr: " + "i".toUpperCase(turkish));

        // the split goes both ways: I lowercases to dotless ı, not i
        System.out.println("tr lower: " + "I".toLowerCase(turkish));

        // no-Locale overloads follow the JVM default locale; results can vary per machine
        System.out.println("default: " + "i".toUpperCase());

        // fixed words such as protocol names must pin Locale.ROOT for casing
        System.out.println("header: " + "content-type".toUpperCase(Locale.ROOT));
    }
}