BreakIterator getText() method in Java with Examples
The getText() method of java.text.BreakIterator class is used to get the text previously set by the setText() method in breakiterator.
Syntax:
public abstract CharacterIterator getText()
Parameter: This method does not accept any parameter.
Return Value: This method provides the text scanned previously.
Below are the examples to illustrate the getText() method:
Example 1:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Java
// Java program to demonstrate getText() methodimport java.text.*;import java.util.*;import java.io.*;public class GFG { public static void main(String[] argv) { // creating and initializing BreakIterator BreakIterator wb = BreakIterator.getWordInstance(); // setting text for BreakIterator wb.setText("Code Geeks"); // getting the text being scanned by // using getText() method StringCharacterIterator text = (StringCharacterIterator)wb.getText(); // display the result System.out.print("Retrieved text is : " + text.first()); for (int i = text.getBeginIndex() - 1; i < text.getEndIndex() - 2; i++) System.out.print(text.next()); }} |
Retrieved text is : Code Geeks
Example 2:
Java
// Java program to demonstrate getText() methodimport java.text.*;import java.util.*;import java.io.*;public class GFG { public static void main(String[] argv) { // creating and initializing BreakIterator BreakIterator wb = BreakIterator.getWordInstance(); // setting text for BreakIterator wb.setText("Geeks For GEeks"); // getting the text being scanned by // using getText() method StringCharacterIterator text = (StringCharacterIterator)wb.getText(); // display the result System.out.print("Retrieved text is : " + text.first()); for (int i = text.getBeginIndex() - 1; i < text.getEndIndex() - 2; i++) System.out.print(text.next()); }} |
Retrieved text is : Geeks For GEeks
Reference: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#getText–


