Java中的 CollationElementIterator setOffset() 方法及示例
Java.text.CollationElementIterator类的setOffset()方法用于将迭代器的光标设置为作为参数传递的特定索引。
句法:
public void setOffset(int newOffset)
参数:此方法采用整数值 newOffset必须设置光标。
返回值:此方法没有任何返回值。
以下是说明setOffset()方法的示例:
示例 1:
Java
// Java program to demonstrate
// setOffset() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing testString
String test = "Code Geeks 123";
// creating and initializing
// RuleBasedCollator object
RuleBasedCollator rbc
= (RuleBasedCollator)(Collator.getInstance());
// creating and initializing
// CollationElementIterator
CollationElementIterator cel
= rbc.getCollationElementIterator(test);
// setting offset to index 4
// using setOffset() method
cel.setOffset(4);
// display the result
System.out.println("current offset is "
+ cel.getOffset());
}
}
Java
// Java program to demonstrate
// setOffset() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing testString
String test = "GeeksForGeeks";
// creating and initializing
// RuleBasedCollator object
RuleBasedCollator rbc
= (RuleBasedCollator)(Collator.getInstance());
// creating and initializing
// CollationElementIterator
CollationElementIterator cel
= rbc.getCollationElementIterator(test);
// after call of setOffset() method
// all next() method will become redundent
cel.next();
cel.next();
cel.next();
cel.next();
// setting cursor of iterator to index 0
// using setOffset() method
cel.setOffset(0);
// display the result
System.out.println("current offset is "
+ cel.getOffset());
}
}
输出:
current offset is 4
示例 2:
Java
// Java program to demonstrate
// setOffset() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing testString
String test = "GeeksForGeeks";
// creating and initializing
// RuleBasedCollator object
RuleBasedCollator rbc
= (RuleBasedCollator)(Collator.getInstance());
// creating and initializing
// CollationElementIterator
CollationElementIterator cel
= rbc.getCollationElementIterator(test);
// after call of setOffset() method
// all next() method will become redundent
cel.next();
cel.next();
cel.next();
cel.next();
// setting cursor of iterator to index 0
// using setOffset() method
cel.setOffset(0);
// display the result
System.out.println("current offset is "
+ cel.getOffset());
}
}
输出:
current offset is 0
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/CollationElementIterator.html#setOffset-int-