The preceding() method of java.text.BreakIterator class is used to get the index of the character of the last boundary preceding the boundary of specified character offset. It provides the offset of the first character of boundary which follows or precedes the current boundary pointed by BreakIterator.
Syntax:
public int preceding(int offset)
Parameter: it takes offset of the character for which the previous boundary has to be found.
Return Value: This method provides the index of boundary which precedes the specified character offset.
Exception: This method throws IllegalArgumentException if the specified index is less than the first text boundary or greater than the last text boundary.
Below are the examples to illustrate the preceding() method:
Example 1:
// Java program to demonstrate preceding() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { // creating and initializing with zero int current = 0 , next = 0 ; // creating and initializing BreakIterator BreakIterator wb = BreakIterator.getWordInstance(); // setting text for BreakIterator wb.setText( "Code Geeks" ); // setting the current position to 11 // by skipping 3 boundaries wb.next( 3 ); // getting the current text boundary current = wb.current(); // display the result System.out.println( "current position before" + " calling preceding() : " + current); // calling preceding() method next = wb.preceding( 6 ); // display the result System.out.println( "\ncurrent position after" + " calling 1st preceding() : " + next); // calling preceding() method next = wb.preceding( 11 ); // display the result System.out.println( "\ncurrent position after" + " calling 2nd preceding() : " + next); } } |
current position before calling preceding() : 11 current position after calling 1st preceding() : 4 current position after calling 2nd preceding() : 6
Example 2:
// Java program to demonstrate preceding() method import java.text.*; import java.util.*; import java.io.*; public class GFG { public static void main(String[] argv) { try { // creating and initializing with zero int current = 0 , next = 0 ; // creating and initializing BreakIterator BreakIterator wb = BreakIterator.getWordInstance(); // setting text for BreakIterator wb.setText( "Code Geeks" ); // setting the current position to 11 // by skipping 3 boundaries wb.next( 3 ); // getting the current text boundary current = wb.current(); // display the result System.out.println( "current position before" + " calling preceding() : " + current); // calling preceding() method next = wb.preceding( 6 ); // display the result System.out.println( "\ncurrent position after" + " calling 1st preceding() : " + next); // calling preceding() method next = wb.preceding(- 1 ); // display the result System.out.println( "\ncurrent position after" + " calling 2nd preceding() : " + next); } catch (IllegalArgumentException e) { System.out.println( "\noffset is less than the first boundary" ); System.out.println( "Exception thrown : " + e); } } } |
current position before calling preceding() : 11 current position after calling 1st preceding() : 4 offset is less than the first boundary Exception thrown : java.lang.IllegalArgumentException: offset out of bounds
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#preceding-int-