Java Locale toString() 方法



描述

Java Locale toString() 方法是獲取整個區域設定的程式設計名稱的 getter,語言、國家/地區和變體之間用下劃線分隔。

宣告

以下是 java.util.Locale.toString() 方法的宣告

public final String toString()

引數

返回值

此方法返回物件的字串表示形式。

異常

獲取 US 區域設定的字串表示形式示例

以下示例顯示了 Java Locale toString() 方法的使用。我們建立了一個 US 區域設定,然後檢索並列印其字串表示形式。

package com.tutorialspoint;

import java.util.Locale;

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

      // create a new locale
      Locale locale = Locale.US;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the string representation of this locale
      System.out.println("String Representation:" + locale.toString());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:en_US
String Representation:en_US

獲取 CANADA 區域設定的字串表示形式示例

以下示例顯示了 Java Locale toString() 方法的使用。我們建立了一個加拿大區域設定,然後檢索並列印其字串表示形式。

package com.tutorialspoint;

import java.util.Locale;

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

      // create a new locale
      Locale locale = Locale.CANADA;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the string representation of this locale
      System.out.println("String Representation:" + locale.toString());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:en_CA
String Representation:en_CA

獲取 FRANCE 區域設定的字串表示形式示例

以下示例顯示了 Java Locale toString() 方法的使用。我們建立了一個法國區域設定,然後檢索並列印其字串表示形式。

package com.tutorialspoint;

import java.util.Locale;

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

      // create a new locale
      Locale locale = Locale.FRANCE;

      // print this locale
      System.out.println("Locale:" + locale);

      // print the string representation of this locale
      System.out.println("String Representation:" + locale.toString());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Locale:fr_FR
String Representation:fr_FR
java_util_locale.htm
廣告

© . All rights reserved.