Java中的 Collator getStrength() 方法与示例
Java.text.Collator 类的getStrength()方法用于获取 Collator 对象的强度属性,这将在比较期间帮助该对象。
句法:
public int getStrength()
参数:此方法不接受任何参数。
返回值:此方法返回 Collator 对象的强度属性,这将在比较期间帮助该对象。
以下是说明getStrength()方法的示例:
示例 1:
Java
// Java program to demonstrate
// getStrength() 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
col.setStrength(Collator.IDENTICAL);
// getting strength property
// using getStrength() method
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
// getStrength() 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);
// getting strength property
// using getStrength() method
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
// getStrength() 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);
// getting strength property
// using getStrength() method
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 :- TERTIARY.
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/Collator.html#getStrength–