Skip to content
Related Articles

Related Articles

BreakIterator getText() method in Java with Examples
  • Last Updated : 27 Nov, 2019

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:




// Java program to demonstrate getText() method
  
import 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("Retrived text is : "
                         + text.first());
        for (int i = text.getBeginIndex() - 1;
             i < text.getEndIndex() - 2;
             i++)
            System.out.print(text.next());
    }
}


Output:

Retrived text is : Code Geeks

Example 2:




// Java program to demonstrate getText() method
  
import 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("Retrived text is : "
                         + text.first());
        for (int i = text.getBeginIndex() - 1;
             i < text.getEndIndex() - 2;
             i++)
            System.out.print(text.next());
    }
}


Output:

Retrived text is : Geeks For GEeks

Reference: https://docs.oracle.com/javase/9/docs/api/java/text/BreakIterator.html#getText–

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :