Java中java.util.Locale.Builder類的setExtension()方法用於將此Locale.Builder設置為指定的擴展鍵。這意味著此方法將設置Locale.Builder實例的當前擴展名,以匹配提供的擴展名和值並返回。如果指定的擴展名為null或為空,則將刪除此LocaleBuilder的擴展。將對照LDML BCP 47兼容擴展名檢查指定的擴展名。
用法:
public Locale.Builder setExtension( char extensionKey, String extensionValue)
參數:此方法接受兩個參數:
- extensionKey:字符值將被設置為此Locale> Builder實例。
- extensionValue:將String值設置為此Locale> Builder實例。
返回值:此方法返回一個Locale.Builder實例,並將此Locale.Builder的擴展名設置為指定的擴展名。
異常:此方法引發以下異常:
- IllformedLocaleException:如果指定的擴展名有任何格式錯誤的字段
程序1:
// Java program to demonstrate
// the above method
import java.util.*;
import java.util.Locale.*;
public class LocaleBuilderDemo {
public static void main(String[] args)
{
// Creating a new Locale.Builder
Locale.Builder localeBuilder
= new Builder();
// Displaying Locale.Builder
System.out.println("LocaleBuilder:"
+ localeBuilder);
// setting the extension of Locale.Builder
char extensionKey = 'u';
String extensionValue = "ca-japanese";
System.out.println("Setting the extension:"
+ extensionKey + "/"
+ extensionValue);
localeBuilder
= localeBuilder
.setExtension(extensionKey,
extensionValue);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder:"
+ localeBuilder);
}
}
輸出:
LocaleBuilder:java.util.Locale$Builder@232204a1 Setting the extension:u/ca-japanese Updated LocaleBuilder:java.util.Locale$Builder@232204a1
程序2:
// Java program to demonstrate
// the above method
import java.util.*;
import java.util.Locale.*;
public class LocaleBuilderDemo {
public static void main(String[] args)
{
// Creating a new Locale.Builder
Locale.Builder localeBuilder
= new Builder();
// Displaying Locale.Builder
System.out.println("LocaleBuilder:"
+ localeBuilder);
// setting the extension of Locale.Builder
char extensionKey = '@';
String extensionValue = "fsdf#";
System.out.println("Setting the extension:"
+ extensionKey + "/"
+ extensionValue);
try {
localeBuilder
= localeBuilder
.setExtension(extensionKey,
extensionValue);
// Displaying Locale.Builder
System.out.println("Updated LocaleBuilder:"
+ localeBuilder);
}
catch (Exception e) {
System.out.println(e);
}
}
}
輸出:
LocaleBuilder:java.util.Locale$Builder@232204a1 Setting the extension:@/fsdf# java.util.IllformedLocaleException: Ill-formed extension key: @ [at index 0]
相關用法
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Clock tickMinutes()用法及代碼示例
- Java Clock withZone()用法及代碼示例
- Java Set add()用法及代碼示例
- Java Set contains()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Locale.Builder setExtension() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。