📌  相关文章
📜  Java中的 StringCharacterIterator setIndex() 方法及示例

📅  最后修改于: 2022-05-13 01:55:34.256000             🧑  作者: Mango

Java中的 StringCharacterIterator setIndex() 方法及示例

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-