Java中java.text.StringCharacterIterator类的setIndex()方法用于设置要由此StringCharacterIterator读取的当前索引。此方法将要设置的索引作为参数,并在该索引处设置此StringCharacterIterator的索引,然后返回该字符。
用法:
public char setIndex(int index)
参数:此方法将要设置的索引作为参数,并设置此StringCharacterIterator的索引。
返回值:此方法返回此方法设置的索引处的字符。
异常:如果指定的索引不在有效范围内(getBeginIndex(),getEndIndex() – 1),则此方法引发IllegalArgumentException。
程序:
// Java program to demonstrate
// the above method
import java.text.*;
import java.util.*;
public class StringCharacterIteratorDemo {
public static void main(String[] args)
{
StringCharacterIterator
stringCharacterIterator
= new StringCharacterIterator(
"GeeksForGeeks");
System.out.println("Current Index: "
+ stringCharacterIterator
.getIndex());
int index = 5;
System.out.println("Updated the index to: "
+ index);
System.out.println("Character at new current index: "
+ stringCharacterIterator
.setIndex(index));
}
}
输出:
Current Index: 0 Updated the index to: 5 Character at new current index: F
参考: https://docs.oracle.com/javase/9/docs/api/java/text/StringCharacterIterator.html#setIndex-int-
相关用法
- Java CharacterIterator setIndex()用法及代码示例
- Java StringCharacterIterator first()用法及代码示例
- Java StringCharacterIterator next()用法及代码示例
- Java StringCharacterIterator last()用法及代码示例
- Java StringCharacterIterator setText()用法及代码示例
- Java StringCharacterIterator previous()用法及代码示例
- Java StringCharacterIterator current()用法及代码示例
- Java StringCharacterIterator getBeginIndex()用法及代码示例
- Java StringCharacterIterator getEndIndex()用法及代码示例
- Java StringCharacterIterator equals()用法及代码示例
- Java StringCharacterIterator getIndex()用法及代码示例
- Java StringCharacterIterator clone()用法及代码示例
- Java StringCharacterIterator hashCode()用法及代码示例
- Java ParsePosition setIndex()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
注:本文由纯净天空筛选整理自ShubhamMaurya3大神的英文原创作品 StringCharacterIterator setIndex() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。