BreakIterator Class

Definition

The BreakIterator class implements methods for finding the location of boundaries in text.

[Android.Runtime.Register("java/text/BreakIterator", DoNotGenerateAcw=true)]
public abstract class BreakIterator : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.Lang.ICloneable
[<Android.Runtime.Register("java/text/BreakIterator", DoNotGenerateAcw=true)>]
type BreakIterator = class
    inherit Object
    interface ICloneable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
BreakIterator
Attributes
Implements

Remarks

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. The CharacterIterator object must not be modified after having been passed to setText. If the text in the CharacterIterator object is changed, the caller must reset BreakIterator by calling setText.

You use the factory methods provided by this class to create instances of various types of break iterators. In particular, use getWordInstance, getLineInstance, getSentenceInstance, and getCharacterInstance 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"> Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. Actual line breaking needs to also consider the available line width and is handled by higher-level software.

"sentence"> 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"> 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"> 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 through character strings, regardless of how the character is stored. The boundaries returned may be those of supplementary characters, combining character sequences, or ligature clusters. 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.

The BreakIterator instances returned by the factory methods of this class are intended for use with natural languages only, not for programming language text. It is however possible to define subclasses that tokenize a programming language.

<strong>Examples</strong>:

Creating and using text boundaries: <blockquote>

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);
                 }
            }

</blockquote>

Print each element in order: <blockquote>

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));
                }
            }

</blockquote>

Print each element in reverse order: <blockquote>

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));
                }
            }

</blockquote>

Print first element: <blockquote>

public static void printFirst(BreakIterator boundary, String source) {
                int start = boundary.first();
                int end = boundary.next();
                System.out.println(source.substring(start,end));
            }

</blockquote>

Print last element: <blockquote>

public static void printLast(BreakIterator boundary, String source) {
                int end = boundary.last();
                int start = boundary.previous();
                System.out.println(source.substring(start,end));
            }

</blockquote>

Print the element at a specified position: <blockquote>

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));
            }

</blockquote>

Find the next word: <blockquote>

{@code
            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.) </blockquote>

Added in 1.1.

Java documentation for java.text.BreakIterator.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

BreakIterator()

Constructor.

BreakIterator(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Fields

Done

DONE is returned by previous(), next(), next(int), preceding(int) and following(int) when either the first or last text boundary has been reached.

Properties

CharacterInstance

Returns a new BreakIterator instance for character breaks for the Locale#getDefault() default locale.

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
LineInstance

Returns a new BreakIterator instance for line breaks for the Locale#getDefault() default locale.

PeerReference (Inherited from Object)
SentenceInstance

Returns a new BreakIterator instance for sentence breaks for the Locale#getDefault() default locale.

Text

Returns a CharacterIterator which represents the text being analyzed.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

WordInstance

Returns a new BreakIterator instance for word breaks for the Locale#getDefault() default locale.

Methods

Clone()

Create a copy of this iterator

Current()

Returns character index of the text boundary that was most recently returned by next(), next(int), previous(), first(), last(), following(int) or preceding(int).

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
First()

Returns the first boundary.

Following(Int32)

Returns the first boundary following the specified character offset.

GetAvailableLocales()

Returns an array of all locales for which the get*Instance methods of this class can return localized instances.

GetCharacterInstance(Locale)

Returns a new BreakIterator instance for character breaks for the given locale.

GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetLineInstance(Locale)

Returns a new BreakIterator instance for line breaks for the given locale.

GetSentenceInstance(Locale)

Returns a new BreakIterator instance for sentence breaks for the given locale.

GetWordInstance(Locale)

Returns a new BreakIterator instance for word breaks for the given locale.

IsBoundary(Int32)

Returns true if the specified character offset is a text boundary.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Last()

Returns the last boundary.

Next()

Returns the boundary following the current boundary.

Next(Int32)

Returns the nth boundary from the current boundary.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Preceding(Int32)

Returns the last boundary preceding the specified character offset.

Previous()

Returns the boundary preceding the current boundary.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
SetText(String)

Set a new text string to be scanned.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to