CollationElementIterator secondaryOrder() method in Java with Examples

2025年5月3日 | 阅读4分钟

java.text.CollationElementIterator 有一个 secondaryOrder() 方法。CollationElementIterator 对象中的每个 Collation 元素都有一个次要(secondary)组件,由 CollationElementIterator 类提供。

语法

参数: 上述方法需要为作为整数参数提供的排序元素(collation element)查找次要组件。

返回值: 该过程返回指定排序元素的次要元素。

示例 1

该代码展示了如何结合使用 CollationElementIterator 和 RuleBasedCollator 来检查字符串中字符的次要排序顺序。RuleBasedCollator 用于定义排序规则,这些规则可以控制字符串比较的处理方式,尤其适用于本地化文本的排序。代码通过对 CollationElementIterator 执行 secondaryOrder() 方法来获取次要排序元素,这些元素通常表示重音符号的差异。这种方法通过区分主要字符差异(如基本字母)和次要差异(如变音符号或大小写),实现了更复杂的字符串比较,使其能够适应文化和语言特定的排序标准。

实施

文件名: SecondaryExample1.java

输出

 
The Secondary order for the order 1 is given by 0
The Secondary order for the order 2 is given by 0
The Secondary order for the order 3 is given by 0
The Secondary order for the order 4 is given by 0
The Secondary order for the order 5 is given by 0
The Secondary order for the order 6 is given by 0
The Secondary order for the order 7 is given by 0
The Secondary order for the order 8 is given by 0
The Secondary order for the order 9 is given by 0
The Secondary order for the order 10 is given by 0   

示例 2

该代码使用 RuleBasedCollator 和 CollationElementIterator 分析字符串的次要排序组件。RuleBasedCollator 实例通过控制特定于区域设置的排序规则,使应用程序能够根据语言惯例来比较和排列文本。代码使用 secondaryOrder() 方法提取次要排序值,这些值通常强调重音符号或变音符号的差异,而不是基本的字符区分。由于次要排序侧重于更细微的字符差异,因此可以确保更精细级别的字符串排序和匹配,这对于执行符合文化习惯的字符串比较至关重要。

实施

文件名: SecondaryExample2.java

输出

 
The Secondary order for the order 1 is given by 0
The Secondary order for the order 2 is given by 0
The Secondary order for the order 3 is given by 0
The Secondary order for the order 4 is given by 0
The Secondary order for the order 5 is given by 0
The Secondary order for the order 6 is given by 1
The Secondary order for the order 7 is given by 0
The Secondary order for the order 8 is given by 0
The Secondary order for the order 9 is given by 0
The Secondary order for the order 10 is given by 0
The Secondary order for the order 11 is given by 0
The Secondary order for the order 12 is given by 0
The Secondary order for the order 13 is given by 0
The Secondary order for the order 14 is given by 1
The Secondary order for the order 15 is given by 0
The Secondary order for the order 16 is given by 0
The Secondary order for the order 17 is given by 1
The Secondary order for the order 18 is given by 0
The Secondary order for the order 19 is given by 0
The Secondary order for the order 20 is given by 0
The Secondary order for the order 21 is given by 0
The Secondary order for the order 22 is given by 0
The Secondary order for the order 23 is given by 1
The Secondary order for the order 24 is given by 0
The Secondary order for the order 25 is given by 0
The Secondary order for the order 26 is given by 0