java.text
Class BreakIterator

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.text.BreakIterator
All Implemented Interfaces:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

public abstract class BreakIterator
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

The BreakIterator class implements methods for finding the location of boundaries in text. Instances of BreakIterator maintain a current position and scan over text returning the index of characters where boundaries occur. Internally, BreakIterator scans text using a CharacterIterator, and is thus able to scan text held by any object implementing that protocol. A StringCharacterIterator is used to scan String objects passed to setText.

You use the factory methods provided by this class to create instances of various types of break iterators. In particular, use getWordIterator, getLineIterator, getSentenceIterator, and getCharacterIterator to create BreakIterators that perform word, line, sentence, and character boundary analysis respectively. A single BreakIterator can work only on one unit (word, line, sentence, and so on). You must use a different iterator for each unit boundary analysis you wish to perform.

Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words.

Sentence boundary analysis allows selection with correct interpretation of periods within numbers and abbreviations, and trailing punctuation marks such as quotation marks and parentheses.

Word boundary analysis is used by search and replace functions, as well as within text editing applications that allow the user to select words with a double click. Word selection provides correct interpretation of punctuation marks within and following words. Characters that are not part of a word, such as symbols or punctuation marks, have word-breaks on both sides.

Character boundary analysis allows users to interact with characters as they expect to, for example, when moving the cursor through a text string. Character boundary analysis provides correct navigation of through character strings, regardless of how the character is stored. For example, an accented character might be stored as a base character and a diacritical mark. What users consider to be a character can differ between languages.

BreakIterator is intended for use with natural languages only. Do not use this class to tokenize a programming language.

Examples:

Creating and using text boundaries

 public static void main(String args[]) {
      if (args.length == 1) {
          String stringToExamine = args[0];
          //print each word in order
          BreakIterator boundary = BreakIterator.getWordInstance();
          boundary.setText(stringToExamine);
          printEachForward(boundary, stringToExamine);
          //print each sentence in reverse order
          boundary = BreakIterator.getSentenceInstance(Locale.US);
          boundary.setText(stringToExamine);
          printEachBackward(boundary, stringToExamine);
          printFirst(boundary, stringToExamine);
          printLast(boundary, stringToExamine);
      }
 }
 
Print each element in order
 public static void printEachForward(BreakIterator boundary, String source) {
     int start = boundary.first();
     for (int end = boundary.next();
          end != BreakIterator.DONE;
          start = end, end = boundary.next()) {
          System.out.println(source.substring(start,end));
     }
 }
 
Print each element in reverse order
 public static void printEachBackward(BreakIterator boundary, String source) {
     int end = boundary.last();
     for (int start = boundary.previous();
          start != BreakIterator.DONE;
          end = start, start = boundary.previous()) {
         System.out.println(source.substring(start,end));
     }
 }
 
Print first element
 public static void printFirst(BreakIterator boundary, String source) {
     int start = boundary.first();
     int end = boundary.next();
     System.out.println(source.substring(start,end));
 }
 
Print last element
 public static void printLast(BreakIterator boundary, String source) {
     int end = boundary.last();
     int start = boundary.previous();
     System.out.println(source.substring(start,end));
 }
 
Print the element at a specified position
 public static void printAt(BreakIterator boundary, int pos, String source) {
     int end = boundary.following(pos);
     int start = boundary.previous();
     System.out.println(source.substring(start,end));
 }
 
Find the next word
 public static int nextWordStartAfter(int pos, String text) {
     BreakIterator wb = BreakIterator.getWordInstance();
     wb.setText(text);
     int last = wb.following(pos);
     int current = wb.next();
     while (current != BreakIterator.DONE) {
         for (int p = last; p < current; p++) {
             if (Character.isLetter(text.codePointAt(p))
                 return last;
         }
         last = current;
         current = wb.next();
     }
     return BreakIterator.DONE;
 }
 
(The iterator returned by BreakIterator.getWordInstance() is unique in that the break positions it returns don't represent both the start and end of the thing being iterated over. That is, a sentence-break iterator returns breaks that each represent the end of one sentence and the beginning of the next. With the word-break iterator, the characters between two boundaries might be a word, or they might be the punctuation or whitespace between two words. The above code uses a simple heuristic to determine which boundary is the beginning of a word: If the characters between this boundary and the next boundary include at least one letter (this can be an alphabetical letter, a CJK ideograph, a Hangul syllable, a Kana character, etc.), then the text between this boundary and the next is a word; otherwise, it's the material between words.)

See Also:
CharacterIterator sample code for java.text.CharacterIterator definition code for java.text.CharacterIterator

Field Summary
static int DONE sample code for java.text.BreakIterator.DONE definition code for java.text.BreakIterator.DONE
          DONE is returned by previous() and next() after all valid boundaries have been returned.
 
Constructor Summary
protected BreakIterator sample code for java.text.BreakIterator.BreakIterator() definition code for java.text.BreakIterator.BreakIterator() ()
          Constructor.
 
Method Summary
 Object sample code for java.lang.Object definition code for java.lang.Object clone sample code for java.text.BreakIterator.clone() definition code for java.text.BreakIterator.clone() ()
          Create a copy of this iterator
abstract  int current sample code for java.text.BreakIterator.current() definition code for java.text.BreakIterator.current() ()
          Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last()
abstract  int first sample code for java.text.BreakIterator.first() definition code for java.text.BreakIterator.first() ()
          Return the first boundary.
abstract  int following sample code for java.text.BreakIterator.following(int) definition code for java.text.BreakIterator.following(int) (int offset)
          Return the first boundary following the specified offset.
static Locale sample code for java.util.Locale definition code for java.util.Locale [] getAvailableLocales sample code for java.text.BreakIterator.getAvailableLocales() definition code for java.text.BreakIterator.getAvailableLocales() ()
          Returns an array of all locales for which the get*Instance methods of this class can return localized instances.
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getCharacterInstance sample code for java.text.BreakIterator.getCharacterInstance() definition code for java.text.BreakIterator.getCharacterInstance() ()
          Create BreakIterator for character-breaks using default locale Returns an instance of a BreakIterator implementing character breaks.
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getCharacterInstance sample code for java.text.BreakIterator.getCharacterInstance(java.util.Locale) definition code for java.text.BreakIterator.getCharacterInstance(java.util.Locale) (Locale sample code for java.util.Locale definition code for java.util.Locale  where)
          Create BreakIterator for character-breaks using specified locale Returns an instance of a BreakIterator implementing character breaks.
protected static int getInt sample code for java.text.BreakIterator.getInt(byte[], int) definition code for java.text.BreakIterator.getInt(byte[], int) (byte[] buf, int offset)
           
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getLineInstance sample code for java.text.BreakIterator.getLineInstance() definition code for java.text.BreakIterator.getLineInstance() ()
          Create BreakIterator for line-breaks using default locale.
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getLineInstance sample code for java.text.BreakIterator.getLineInstance(java.util.Locale) definition code for java.text.BreakIterator.getLineInstance(java.util.Locale) (Locale sample code for java.util.Locale definition code for java.util.Locale  where)
          Create BreakIterator for line-breaks using specified locale.
protected static long getLong sample code for java.text.BreakIterator.getLong(byte[], int) definition code for java.text.BreakIterator.getLong(byte[], int) (byte[] buf, int offset)
           
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getSentenceInstance sample code for java.text.BreakIterator.getSentenceInstance() definition code for java.text.BreakIterator.getSentenceInstance() ()
          Create BreakIterator for sentence-breaks using default locale Returns an instance of a BreakIterator implementing sentence breaks.
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getSentenceInstance sample code for java.text.BreakIterator.getSentenceInstance(java.util.Locale) definition code for java.text.BreakIterator.getSentenceInstance(java.util.Locale) (Locale sample code for java.util.Locale definition code for java.util.Locale  where)
          Create BreakIterator for sentence-breaks using specified locale Returns an instance of a BreakIterator implementing sentence breaks.
protected static short getShort sample code for java.text.BreakIterator.getShort(byte[], int) definition code for java.text.BreakIterator.getShort(byte[], int) (byte[] buf, int offset)
           
abstract  CharacterIterator sample code for java.text.CharacterIterator definition code for java.text.CharacterIterator getText sample code for java.text.BreakIterator.getText() definition code for java.text.BreakIterator.getText() ()
          Get the text being scanned
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getWordInstance sample code for java.text.BreakIterator.getWordInstance() definition code for java.text.BreakIterator.getWordInstance() ()
          Create BreakIterator for word-breaks using default locale.
static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator getWordInstance sample code for java.text.BreakIterator.getWordInstance(java.util.Locale) definition code for java.text.BreakIterator.getWordInstance(java.util.Locale) (Locale sample code for java.util.Locale definition code for java.util.Locale  where)
          Create BreakIterator for word-breaks using specified locale.
 boolean isBoundary sample code for java.text.BreakIterator.isBoundary(int) definition code for java.text.BreakIterator.isBoundary(int) (int offset)
          Return true if the specified position is a boundary position.
abstract  int last sample code for java.text.BreakIterator.last() definition code for java.text.BreakIterator.last() ()
          Return the last boundary.
abstract  int next sample code for java.text.BreakIterator.next() definition code for java.text.BreakIterator.next() ()
          Return the boundary following the current boundary.
abstract  int next sample code for java.text.BreakIterator.next(int) definition code for java.text.BreakIterator.next(int) (int n)
          Return the nth boundary from the current boundary
 int preceding sample code for java.text.BreakIterator.preceding(int) definition code for java.text.BreakIterator.preceding(int) (int offset)
          Return the last boundary preceding the specfied offset.
abstract  int previous sample code for java.text.BreakIterator.previous() definition code for java.text.BreakIterator.previous() ()
          Return the boundary preceding the current boundary.
abstract  void setText sample code for java.text.BreakIterator.setText(java.text.CharacterIterator) definition code for java.text.BreakIterator.setText(java.text.CharacterIterator) (CharacterIterator sample code for java.text.CharacterIterator definition code for java.text.CharacterIterator  newText)
          Set a new text for scanning.
 void setText sample code for java.text.BreakIterator.setText(java.lang.String) definition code for java.text.BreakIterator.setText(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  newText)
          Set a new text string to be scanned.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Field Detail

DONE sample code for java.text.BreakIterator.DONE

public static final int DONE
DONE is returned by previous() and next() after all valid boundaries have been returned.

See Also:
Constant Field Values
Constructor Detail

BreakIterator sample code for java.text.BreakIterator() definition code for java.text.BreakIterator()

protected BreakIterator()
Constructor. BreakIterator is stateless and has no default behavior.

Method Detail

clone sample code for java.text.BreakIterator.clone() definition code for java.text.BreakIterator.clone()

public Object sample code for java.lang.Object definition code for java.lang.Object  clone()
Create a copy of this iterator

Overrides:
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() in class Object sample code for java.lang.Object definition code for java.lang.Object
Returns:
A copy of this
See Also:
Cloneable sample code for java.lang.Cloneable definition code for java.lang.Cloneable

first sample code for java.text.BreakIterator.first() definition code for java.text.BreakIterator.first()

public abstract int first()
Return the first boundary. The iterator's current position is set to the first boundary.

Returns:
The character index of the first text boundary.

last sample code for java.text.BreakIterator.last() definition code for java.text.BreakIterator.last()

public abstract int last()
Return the last boundary. The iterator's current position is set to the last boundary.

Returns:
The character index of the last text boundary.

next sample code for java.text.BreakIterator.next(int) definition code for java.text.BreakIterator.next(int)

public abstract int next(int n)
Return the nth boundary from the current boundary

Parameters:
n - which boundary to return. A value of 0 does nothing. Negative values move to previous boundaries and positive values move to later boundaries.
Returns:
The index of the nth boundary from the current position.

next sample code for java.text.BreakIterator.next() definition code for java.text.BreakIterator.next()

public abstract int next()
Return the boundary following the current boundary.

Returns:
The character index of the next text boundary or DONE if all boundaries have been returned. Equivalent to next(1).

previous sample code for java.text.BreakIterator.previous() definition code for java.text.BreakIterator.previous()

public abstract int previous()
Return the boundary preceding the current boundary.

Returns:
The character index of the previous text boundary or DONE if all boundaries have been returned.

following sample code for java.text.BreakIterator.following(int) definition code for java.text.BreakIterator.following(int)

public abstract int following(int offset)
Return the first boundary following the specified offset. The value returned is always greater than the offset or the value BreakIterator.DONE

Parameters:
offset - the offset to begin scanning. Valid values are determined by the CharacterIterator passed to setText(). Invalid values cause an IllegalArgumentException to be thrown.
Returns:
The first boundary after the specified offset.

preceding sample code for java.text.BreakIterator.preceding(int) definition code for java.text.BreakIterator.preceding(int)

public int preceding(int offset)
Return the last boundary preceding the specfied offset. The value returned is always less than the offset or the value BreakIterator.DONE.

Parameters:
offset - the offset to begin scanning. Valid values are determined by the CharacterIterator passed to setText(). Invalid values cause an IllegalArgumentException to be thrown.
Returns:
The last boundary before the specified offset.
Since:
1.2

isBoundary sample code for java.text.BreakIterator.isBoundary(int) definition code for java.text.BreakIterator.isBoundary(int)

public boolean isBoundary(int offset)
Return true if the specified position is a boundary position.

Parameters:
offset - the offset to check.
Returns:
True if "offset" is a boundary position.
Since:
1.2

current sample code for java.text.BreakIterator.current() definition code for java.text.BreakIterator.current()

public abstract int current()
Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last()

Returns:
The boundary most recently returned.

getText sample code for java.text.BreakIterator.getText() definition code for java.text.BreakIterator.getText()

public abstract CharacterIterator sample code for java.text.CharacterIterator definition code for java.text.CharacterIterator  getText()
Get the text being scanned

Returns:
the text being scanned

setText sample code for java.text.BreakIterator.setText(java.lang.String) definition code for java.text.BreakIterator.setText(java.lang.String)

public void setText(String sample code for java.lang.String definition code for java.lang.String  newText)
Set a new text string to be scanned. The current scan position is reset to first().

Parameters:
newText - new text to scan.

setText sample code for java.text.BreakIterator.setText(java.text.CharacterIterator) definition code for java.text.BreakIterator.setText(java.text.CharacterIterator)

public abstract void setText(CharacterIterator sample code for java.text.CharacterIterator definition code for java.text.CharacterIterator  newText)
Set a new text for scanning. The current scan position is reset to first().

Parameters:
newText - new text to scan.

getWordInstance sample code for java.text.BreakIterator.getWordInstance() definition code for java.text.BreakIterator.getWordInstance()

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getWordInstance()
Create BreakIterator for word-breaks using default locale. Returns an instance of a BreakIterator implementing word breaks. WordBreak is usefull for word selection (ex. double click)

Returns:
A BreakIterator for word-breaks
See Also:
Locale.getDefault() sample code for java.util.Locale.getDefault() definition code for java.util.Locale.getDefault()

getWordInstance sample code for java.text.BreakIterator.getWordInstance(java.util.Locale) definition code for java.text.BreakIterator.getWordInstance(java.util.Locale)

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getWordInstance(Locale sample code for java.util.Locale definition code for java.util.Locale  where)
Create BreakIterator for word-breaks using specified locale. Returns an instance of a BreakIterator implementing word breaks. WordBreak is usefull for word selection (ex. double click)

Parameters:
where - the local. If a specific WordBreak is not avaliable for the specified locale, a default WordBreak is returned.
Returns:
A BreakIterator for word-breaks

getLineInstance sample code for java.text.BreakIterator.getLineInstance() definition code for java.text.BreakIterator.getLineInstance()

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getLineInstance()
Create BreakIterator for line-breaks using default locale. Returns an instance of a BreakIterator implementing line breaks. Line breaks are logically possible line breaks, actual line breaks are usually determined based on display width. LineBreak is useful for word wrapping text.

Returns:
A BreakIterator for line-breaks
See Also:
Locale.getDefault() sample code for java.util.Locale.getDefault() definition code for java.util.Locale.getDefault()

getLineInstance sample code for java.text.BreakIterator.getLineInstance(java.util.Locale) definition code for java.text.BreakIterator.getLineInstance(java.util.Locale)

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getLineInstance(Locale sample code for java.util.Locale definition code for java.util.Locale  where)
Create BreakIterator for line-breaks using specified locale. Returns an instance of a BreakIterator implementing line breaks. Line breaks are logically possible line breaks, actual line breaks are usually determined based on display width. LineBreak is useful for word wrapping text.

Parameters:
where - the local. If a specific LineBreak is not avaliable for the specified locale, a default LineBreak is returned.
Returns:
A BreakIterator for line-breaks

getCharacterInstance sample code for java.text.BreakIterator.getCharacterInstance() definition code for java.text.BreakIterator.getCharacterInstance()

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getCharacterInstance()
Create BreakIterator for character-breaks using default locale Returns an instance of a BreakIterator implementing character breaks. Character breaks are boundaries of combining character sequences.

Returns:
A BreakIterator for character-breaks
See Also:
Locale.getDefault() sample code for java.util.Locale.getDefault() definition code for java.util.Locale.getDefault()

getCharacterInstance sample code for java.text.BreakIterator.getCharacterInstance(java.util.Locale) definition code for java.text.BreakIterator.getCharacterInstance(java.util.Locale)

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getCharacterInstance(Locale sample code for java.util.Locale definition code for java.util.Locale  where)
Create BreakIterator for character-breaks using specified locale Returns an instance of a BreakIterator implementing character breaks. Character breaks are boundaries of combining character sequences.

Parameters:
where - the local. If a specific character break is not avaliable for the specified local, a default character break is returned.
Returns:
A BreakIterator for character-breaks

getSentenceInstance sample code for java.text.BreakIterator.getSentenceInstance() definition code for java.text.BreakIterator.getSentenceInstance()

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getSentenceInstance()
Create BreakIterator for sentence-breaks using default locale Returns an instance of a BreakIterator implementing sentence breaks.

Returns:
A BreakIterator for sentence-breaks
See Also:
Locale.getDefault() sample code for java.util.Locale.getDefault() definition code for java.util.Locale.getDefault()

getSentenceInstance sample code for java.text.BreakIterator.getSentenceInstance(java.util.Locale) definition code for java.text.BreakIterator.getSentenceInstance(java.util.Locale)

public static BreakIterator sample code for java.text.BreakIterator definition code for java.text.BreakIterator  getSentenceInstance(Locale sample code for java.util.Locale definition code for java.util.Locale  where)
Create BreakIterator for sentence-breaks using specified locale Returns an instance of a BreakIterator implementing sentence breaks.

Parameters:
where - the local. If a specific SentenceBreak is not avaliable for the specified local, a default SentenceBreak is returned.
Returns:
A BreakIterator for sentence-breaks

getAvailableLocales sample code for java.text.BreakIterator.getAvailableLocales() definition code for java.text.BreakIterator.getAvailableLocales()

public static Locale sample code for java.util.Locale definition code for java.util.Locale [] getAvailableLocales()
Returns an array of all locales for which the get*Instance methods of this class can return localized instances. The array returned must contain at least a Locale instance equal to Locale.US sample code for java.util.Locale.US definition code for java.util.Locale.US .

Returns:
An array of locales for which localized BreakIterator instances are available.

getLong sample code for java.text.BreakIterator.getLong(byte[], int) definition code for java.text.BreakIterator.getLong(byte[], int)

protected static long getLong(byte[] buf,
                              int offset)

getInt sample code for java.text.BreakIterator.getInt(byte[], int) definition code for java.text.BreakIterator.getInt(byte[], int)

protected static int getInt(byte[] buf,
                            int offset)

getShort sample code for java.text.BreakIterator.getShort(byte[], int) definition code for java.text.BreakIterator.getShort(byte[], int)

protected static short getShort(byte[] buf,
                                int offset)