Open In App

BreakIterator getText() method in Java with Examples

Last Updated : 27 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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("Retrieved text is : "
                         + text.first());
        for (int i = text.getBeginIndex() - 1;
             i < text.getEndIndex() - 2;
             i++)
            System.out.print(text.next());
    }
}


Output

Retrieved text is : Code Geeks

Example 2: 
 

Java




// 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("Retrieved text is : "
                         + text.first());
        for (int i = text.getBeginIndex() - 1;
             i < text.getEndIndex() - 2;
             i++)
            System.out.print(text.next());
    }
}


Output

Retrieved text is : Geeks For GEeks

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads