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


Java BreakIterator following()用法及代碼示例


java.text.BreakIterator類的following()方法用於返回在文本行中指定偏移量之後出現的第一個邊界的索引。它提供跟隨傳遞的偏移量邊界的下一個邊界的第一個字符的偏移量。

用法:

public abstract int following(int offset)

參數:此方法將偏移量作為參數,必須為其找到所需邊界,該邊界位於第一個邊界之後。


返回值:此方法在指定的偏移量之後提供第一個邊界。

異常:如果offset小於第一個邊界且大於最後一個邊界,則此方法引發IllegalArgumentException。

下麵是說明following()方法的示例:

示例1:

// Java program to demonstrate following() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
            int current = 0; 
  
            // creating and initializing BreakIterator 
            BreakIterator wb 
                = BreakIterator.getWordInstance(); 
  
            // setting text for BreakIterator 
            wb.setText("Code  Geeks"); 
  
            // getting the text boundary 
            current = wb.following(0); 
  
            // display the result 
            System.out.println( 
                "first boundary for offset 0 : "
                + current); 
  
            // getting the text boundary 
            current = wb.following(4); 
  
            // display the result 
            System.out.println( 
                "\nfirst boundary for offset 4 : "
                + current); 
  
            // getting the text boundary 
            current = wb.following(8); 
  
            // display the result 
            System.out.println( 
                "\nfirst boundary for offset 8 : "
                + current); 
        } 
  
        catch (IllegalArgumentException e) { 
  
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
first boundary for offset 0 : 4

first boundary for offset 4 : 6

first boundary for offset 8 : 11

示例2:

// Java program to demonstrate following() method 
  
import java.text.*; 
import java.util.*; 
import java.io.*; 
  
public class GFG { 
    public static void main(String[] argv) 
    { 
        try { 
            int current = 0; 
  
            // creating and initializing BreakIterator 
            BreakIterator wb 
                = BreakIterator.getWordInstance(); 
  
            // setting text for BreakIterator 
            wb.setText("Code  Geeks"); 
  
            // getting the text boundary 
            current = wb.following(0); 
  
            // display the result 
            System.out.println( 
                "first boundary for offset 0 : "
                + current); 
  
            // getting the text boundary 
            current = wb.following(4); 
  
            // display the result 
            System.out.println( 
                "\nfirst boundary for offset 4 : "
                + current); 
  
            // getting the text boundary 
            current = wb.following(-8); 
  
            // display the result 
            System.out.println( 
                "\nfirst boundary for offset 8 : "
                + current); 
        } 
  
        catch (IllegalArgumentException e) { 
            System.out.println( 
                "\noffset is less than"
                + " the first boundary"); 
            System.out.println("Exception thrown : " + e); 
        } 
    } 
}
輸出:
first boundary for offset 0 : 4

first boundary for offset 4 : 6

offset is less than the first boundary
Exception thrown : java.lang.IllegalArgumentException: offset out of bounds

參考: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#following-int-



相關用法


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