Java中的 CharacterIterator getIndex() 方法及示例
Java中Java .text.CharacterIterator接口的getIndex()方法用于获取该CharacterIterator要读取的当前索引。此方法返回该索引号。
句法:
public int getIndex()
参数:此方法不接受任何参数。
返回值:此方法返回此 CharacterIterator 将要读取的索引号。
异常:此方法不抛出任何异常。
程序:
// Java program to demonstrate
// the above method
import java.text.*;
import java.util.*;
public class CharacterIteratorDemo {
public static void main(String[] args)
{
CharacterIterator characterIterator
= new StringCharacterIterator(
"GeeksForGeeks");
System.out.println("Current character: "
+ characterIterator
.current());
System.out.println("Current Index: "
+ characterIterator
.getIndex());
}
}
输出:
Current character: G
Current Index: 0
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/CharacterIterator.html#getIndex–