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

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

Java中的 BreakIterator following() 方法及示例

Java.text.BreakIterator类的following()方法用于返回文本行中指定偏移量之后出现的第一个边界的索引。它提供了在传递的偏移边界之后的下一个边界的第一个字符的偏移量。
句法:

public abstract int following(int offset)

参数:此方法将偏移量作为参数,必须找到第一个边界之后的所需边界。
返回值:此方法提供指定偏移量之后的第一个边界。
异常:如果偏移量小于第一个边界且大于最后一个边界,则此方法抛出IllegalArgumentException
以下是说明以下()方法的示例:
示例 1:

Java
// 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);
        }
    }
}


Java
// 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

first boundary for offset 8 : 11

示例 2:

Java

// 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-