El método anterior() de la clase java.text.BreakIterator se usa para obtener el índice del límite anterior detrás del límite actual (límite recuperado llamando al método actual()). Proporciona el desplazamiento del primer carácter del límite que precede al límite actual señalado por BreakIterator.
Sintaxis:
public abstract int previous()
Parámetro: Este método no acepta ningún parámetro.
Valor de retorno: este método proporciona el índice del límite anterior que sigue al límite actual.
A continuación se muestran los ejemplos para ilustrar el método anterior() :
Ejemplo 1:
// Java program to demonstrate previous() 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 index 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 previous() : " + current); // calling previous() method next = wb.previous(); // display the result System.out.println( "\ncurrent position after" + " calling 1st previous() : " + next); // calling previous() method next = wb.previous(); // display the result System.out.println( "\ncurrent position after" + " calling 2nd previous() : " + next); } }
current position before calling previous() : 11 current position after calling 1st previous() : 6 current position after calling 2nd previous() : 4
Ejemplo 2:
// Java program to demonstrate // previous() 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("GeeksForGeeks"); // setting the current position to index 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 previous() : " + current); // calling previous() method next = wb.previous(); // display the result System.out.println( "\ncurrent position after" + " calling 1st previous() : " + next); // calling previous() method next = wb.previous(); // display the result System.out.println( "\ncurrent position after" + " calling 2nd previous() : " + next); } }
current position before calling previous() : 13 current position after calling 1st previous() : 0 current position after calling 2nd previous() : -1
Referencia: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#previous–
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA