当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。