Determining the Character Boundaries in a Unicode String


 

Determining the Character Boundaries in a Unicode String

In this section,  you will learn how to determine the character boundaries in a string.

In this section,  you will learn how to determine the character boundaries in a string.

Determining the Character Boundaries in a Unicode String

In this section,  you will learn how to determine the character boundaries in a string.

By determining the character boundaries, you will be able to break the string into characters. The class BreakIterator enables the user to find the location of boundaries in the text by providing some useful methods. The instances of this class returns the index of characters boundaries by maintaining the current position and scan over the text.

In the given example, we have invoked the factory method getCharacterInstance() and passed a string 'HelloWorld' to the setText() method. The method getCharactersInstance() create breakIterator for character-breaks using default locale and returns an instance of a BreakIterator implementing character breaks. The setText() method set the text string to be scanned. Then, we have created a loop to find the location of character boundary from the string and break the text into different characters.

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.*;
import java.util.Locale;

public class CharacterBoundaries {
	public static void main(String[] args) {
		String str = "", st = "HelloWorld";
		BreakIterator bi = BreakIterator.getCharacterInstance(Locale.FRANCE);
		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:

H
e
l
l
o
W
o
r
l
d

Ads