java.text.ChoiceFormat類的applyPattern()方法用於通過覆蓋當前限製和格式來為當前ChoiceFormat設置新的模式文本。此新模式將是ChoiceFormat的限製和格式的組合
用法:
public void applyPattern(String newPattern)
參數:此方法將newPattern作為參數,這是ChoiceFormat的新文本模式。
返回值:此方法不返回任何內容。
異常:如果指定的newPattern為null,則此方法引發NullPointerException。
下麵是說明applyPattern()方法的示例:
示例1:
// Java program to demonstrate applyPattern() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing limit
double[] limit = { 1, 2, 3 };
// creating and initializing format
String[] format = { "sun", "mon", "tue" };
// creating and initializing ChoiceFormat
ChoiceFormat cf
= new ChoiceFormat(limit, format);
// display the result
System.out.println("current pattern : "
+ cf.toPattern());
// applying the new pattern
// using applyPattern() method
cf.applyPattern("4#wed| 5#thu | 6#fri | 7#sat");
// display the result
System.out.println("\nnew pattern : "
+ cf.toPattern());
}
}
輸出:
current pattern : 1.0#sun|2.0#mon|3.0#tue new pattern : 4.0#wed|5.0#thu |6.0#fri |7.0#sat
示例2:
// Java program to demonstrate applyPattern() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// creating and initializing limit
double[] limit = { 1, 2, 3 };
// creating and initializing format
String[] format = { "sun", "mon", "tue" };
// creating and initializing ChoiceFormat
ChoiceFormat cf
= new ChoiceFormat(limit, format);
// display the result
System.out.println("current pattern : "
+ cf.toPattern());
// applying the new pattern
// using applyPattern() method
cf.applyPattern(null);
// display the result
System.out.println("\nnew pattern : "
+ cf.toPattern());
}
catch (NullPointerException e) {
System.out.println("\nString is Null");
System.out.println("Exception thrown : " + e);
}
}
}
輸出:
current pattern : 1.0#sun|2.0#mon|3.0#tue String is Null Exception thrown : java.lang.NullPointerException
參考: https://docs.oracle.com/javase/9/docs/api/java/text/ChoiceFormat.html#applyPattern-java.lang.String-
相關用法
- Java SimpleDateFormat applyPattern()用法及代碼示例
- Java ChoiceFormat setChoices()用法及代碼示例
- Java ChoiceFormat parse()用法及代碼示例
- Java ChoiceFormat format()用法及代碼示例
- Java ChoiceFormat previousDouble()用法及代碼示例
- Java ChoiceFormat getLimits()用法及代碼示例
- Java ChoiceFormat hashCode()用法及代碼示例
- Java ChoiceFormat equals()用法及代碼示例
- Java ChoiceFormat getFormats()用法及代碼示例
- Java ChoiceFormat nextDouble(double)用法及代碼示例
- Java ChoiceFormat nextDouble(double, boolean)用法及代碼示例
- Java MessageFormat applyPattern()用法及代碼示例
- Java DecimalFormat applyPattern()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 ChoiceFormat applyPattern() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。