Determining Potential Line Breaks in a Unicode String


 

Determining Potential Line Breaks in a Unicode String

This section illustrates you how to use the BreakIterator to parse a string on a line break.

This section illustrates you how to use the BreakIterator to parse a string on a line break.

Determining Potential Line Breaks in a Unicode String

This section illustrates you how to use the BreakIterator to parse a string on a line break.

Parsing a text is a common task. Through the use of BreakIterator class, you can perform parsing in a language-independent manner as it is locale sensitive. This class provides factory methods to create instances of various types of break iterators. Here we are going to break a text string  on the basis of line.

You can see in the given example, we have invoked the factory method getLineInstance() of BreakIterator class and passed a string 'Hello\nWorld' to the setText() method. The method getLineInstance() create BreakIterator for line-breaks using default locale and setText() method set the text string to be scanned. Then, we have created a loop to find the location of new line character (\n) from the string and break the text from there into different lines.

current(): This method of BreakIterator class return character index of the text boundary that was most recently returned.

next(): This method of BreakIterator class return the boundary following the current boundary.

Here is the code:

import java.text.*;

public class LineBreak {
	public static void main(String[] args) {
		String str = "", st = "Hello\nWorld";
		BreakIterator bi = BreakIterator.getLineInstance();
		bi.setText(st);
		int index = 0;
		while (bi.next() != BreakIterator.DONE) {
			str = st.substring(index, bi.current());
			System.out.println(str);
			index = bi.current();
		}
	}
}

Output:

Hello

World

Ads