Java的Locale toString() 方法和示例
Java中Locale 类的toString()方法用于返回此语言环境的字符串表示形式。每个元素,即语言、国家或地区的变体都用下划线分隔
句法:
LOCALE.toString()
参数:该方法不带任何参数。
返回值:此方法返回此语言环境的字符串表示形式。
下面的程序说明了 toString() 方法的工作:
方案一:
// Java code to illustrate toString() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("nu", "NO", "NY");
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Displaying the string of this locale
System.out.println("The String: "
+ first_locale.toString());
}
}
输出:
First Locale: nu_NO_NY
The String: nu_NO_NY
方案二:
// Java code to illustrate toString() method
import java.util.*;
public class Locale_Demo {
public static void main(String[] args)
{
// Creating a new locale
Locale first_locale
= new Locale("ar", "SA");
// Displaying first locale
System.out.println("First Locale: "
+ first_locale);
// Displaying the string of this locale
System.out.println("The String: "
+ first_locale.toString());
}
}
输出:
First Locale: ar_SA
The String: ar_SA