當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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-



相關用法


注:本文由純淨天空篩選整理自ShubhamMaurya3大神的英文原創作品 StringCharacterIterator setIndex() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。