Java BreakIterator following()方法及实例
java.text.BreakIterator 类的 following() 方法用于返回文本行中指定偏移量之后的第一个边界的索引。它提供了在所传递的偏移量的边界之后的下一个边界的第一个字符的偏移量。
语法
public abstract int following(int 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 : 4first boundary for offset 4 : 6first 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 : 4first boundary for offset 4 : 6offset is less than the first boundaryException thrown : java.lang.IllegalArgumentException: offset out of bounds
参考资料: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#following-int-
