Java 中的 Locale getExtensionKeys()方法,示例

原文:https://www . geesforgeks . org/locale-getextensionkeys-method-in-Java-with-examples/

Java 中 Locale 类getExtensionKeys() 方法用来返回一组与这个 Locale 相关联的扩展键,这些扩展键不能被修改,并且键保持小写。如果没有扩展名,则该方法返回一个空集合。

语法:

public Set <Character> getExtensionKeys()

参数:该方法不取任何参数。

返回值:该方法返回一组与该区域设置相关联的扩展键,如果没有扩展,则返回一个空集。

以下程序说明了 getExtensionKeys()方法的工作: 程序 1:

// Java code to illustrate
// getExtensionKeys() method

import java.util.*;

public class Locale_Demo {
    public static void main(String[] args)
    {

        // Creating a new locale
        Locale first_locale
            = new Locale("th", "TH", "TH");

        // Displaying first locale
        System.out.println("Locale: "
                           + first_locale);

        // Displaying the set
        System.out.println("The KeySet: "
                           + first_locale.getExtensionKeys());
    }
}

Output:

Locale: th_TH_TH_#u-nu-thai
The KeySet: [u]

程序 2:

// Java code to illustrate
// getExtensionKeys() method

import java.util.*;

public class Locale_Demo {
    public static void main(String[] args)
    {

        // Creating a new locale
        Locale first_locale
            = new Locale("en", "US");

        // Displaying first locale
        System.out.println("Locale: "
                           + first_locale);

        // Displaying the set
        System.out.println("The KeySet: "
                           + first_locale.getExtensionKeys());
    }
}

Output:

Locale: en_US
The KeySet: []

参考:https://docs . Oracle . com/javase/7/docs/API/Java/util/locale . html # getExtensionKeys()