Java中的 Collator setStrength() 方法与示例
Java.text.Collator 类的setStrength()方法用于设置 Collator 对象的强度属性,这将在比较两个字符串时帮助该对象。
句法:
public void setStrength(int newStrength)
参数:此方法接受 Collator 对象的强度属性作为参数,这将在比较过程中帮助该对象。
返回值:此方法没有任何返回值。
以下是说明setStrength()方法的示例:
示例 1:
Java
// Java program to demonstrate
// setStrength() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing new simple rule
String simple = "< a < b < c < d";
// Creating and initializing
// new RuleBasedCollator Object
RuleBasedCollator col
= new RuleBasedCollator(simple);
// setting strength property for the Collator object
// using getStrength() method
col.setStrength(Collator.IDENTICAL);
// getting strength property
int strength = col.getStrength();
// display result
if (strength == 0)
System.out.println(
"current strength"
+ " property :- PRIMARY.");
else if (strength == 1)
System.out.println(
"current strength"
+ " property :- SECONDARY.");
else if (strength == 2)
System.out.println(
"current strength"
+ " property :- TERTIARY.");
else
System.out.println(
"current strength"
+ " property :- IDENTICAL.");
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
catch (ParseException e) {
System.out.println("Exception thrown : " + e);
}
}
}
Java
// Java program to demonstrate
// setStrength() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing
// Collator Object
Collator col
= Collator.getInstance(Locale.UK);
// setting strength property
// for the Collator object
// using getStrength() method
col.setStrength(Collator.PRIMARY);
// getting strength property
int strength = col.getStrength();
// display result
if (strength == 0)
System.out.println(
"current strength"
+ " property :- PRIMARY.");
else if (strength == 1)
System.out.println(
"current strength"
+ " property :- SECONDARY.");
else if (strength == 2)
System.out.println(
"current strength"
+ " property :- TERTIARY.");
else
System.out.println(
"current strength"
+ " property :- IDENTICAL.");
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
current strength property :- IDENTICAL.
示例 2:
Java
// Java program to demonstrate
// setStrength() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing
// Collator Object
Collator col
= Collator.getInstance(Locale.UK);
// setting strength property
// for the Collator object
// using getStrength() method
col.setStrength(Collator.PRIMARY);
// getting strength property
int strength = col.getStrength();
// display result
if (strength == 0)
System.out.println(
"current strength"
+ " property :- PRIMARY.");
else if (strength == 1)
System.out.println(
"current strength"
+ " property :- SECONDARY.");
else if (strength == 2)
System.out.println(
"current strength"
+ " property :- TERTIARY.");
else
System.out.println(
"current strength"
+ " property :- IDENTICAL.");
}
catch (ClassCastException e) {
System.out.println("Exception thrown : " + e);
}
}
}
输出:
current strength property :- PRIMARY.
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/Collator.html#setStrength-int-