java.lang.Objectjava.text.BreakIterator
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
Print each element in order
if (args.length == 1) { //print each word in order boundary.setText(stringToExamine); printEachForward(boundary, stringToExamine); //print each sentence in reverse order boundary.setText(stringToExamine); printEachBackward(boundary, stringToExamine); printFirst(boundary, stringToExamine); printLast(boundary, stringToExamine); } }
Print each element in reverse order
int start = boundary.first(); for (int end = boundary.next(); start = end, end = boundary.next()) { } }
Print first element
int end = boundary.last(); for (int start = boundary.previous(); end = start, start = boundary.previous()) { } }
Print last element
int start = boundary.first(); int end = boundary.next(); }
Print the element at a specified position
int end = boundary.last(); int start = boundary.previous(); }
Find the next word
int end = boundary.following(pos); int start = boundary.previous(); }
(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.)
wb.setText(text); int last = wb.following(pos); int current = wb.next(); for (int p = last; p < current; p++) { return last; } last = current; current = wb.next(); } }
CharacterIterator| Field Summary | |
|---|---|
static int |
DONE
DONE is returned by previous() and next() after all valid boundaries have been returned. |
| Constructor Summary | |
|---|---|
protected |
BreakIterator()
Constructor. |
| Method Summary | |
|---|---|
Object |
clone()
Create a copy of this iterator |
abstract int |
current()
Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last() |
abstract int |
first()
Return the first boundary. |
abstract int |
following(int offset)
Return the first boundary following the specified offset. |
static Locale[] |
getAvailableLocales()
Returns an array of all locales for which the get*Instance methods of this class can return
localized instances. |
static BreakIterator |
getCharacterInstance()
Create BreakIterator for character-breaks using default locale Returns an instance of a BreakIterator implementing character breaks. |
static BreakIterator |
getCharacterInstance(Locale where)
Create BreakIterator for character-breaks using specified locale Returns an instance of a BreakIterator implementing character breaks. |
protected static int |
getInt(byte[] buf,
int offset)
|
static BreakIterator |
getLineInstance()
Create BreakIterator for line-breaks using default locale. |
static BreakIterator |
getLineInstance(Locale where)
Create BreakIterator for line-breaks using specified locale. |
protected static long |
getLong(byte[] buf,
int offset)
|
static BreakIterator |
getSentenceInstance()
Create BreakIterator for sentence-breaks using default locale Returns an instance of a BreakIterator implementing sentence breaks. |
static BreakIterator |
getSentenceInstance(Locale where)
Create BreakIterator for sentence-breaks using specified locale Returns an instance of a BreakIterator implementing sentence breaks. |
protected static short |
getShort(byte[] buf,
int offset)
|
abstract CharacterIterator |
getText()
Get the text being scanned |
static BreakIterator |
getWordInstance()
Create BreakIterator for word-breaks using default locale. |
static BreakIterator |
getWordInstance(Locale where)
Create BreakIterator for word-breaks using specified locale. |
boolean |
isBoundary(int offset)
Return true if the specified position is a boundary position. |
abstract int |
last()
Return the last boundary. |
abstract int |
next()
Return the boundary following the current boundary. |
abstract int |
next(int n)
Return the nth boundary from the current boundary |
int |
preceding(int offset)
Return the last boundary preceding the specfied offset. |
abstract int |
previous()
Return the boundary preceding the current boundary. |
abstract void |
setText(CharacterIterator newText)
Set a new text for scanning. |
void |
setText(String newText)
Set a new text string to be scanned. |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int DONE
| Constructor Detail |
|---|
protected BreakIterator()
| Method Detail |
|---|
public Object clone()
Cloneablepublic abstract int first()
public abstract int last()
public abstract int next(int n)
n - which boundary to return. A value of 0
does nothing. Negative values move to previous boundaries
and positive values move to later boundaries.
public abstract int next()
public abstract int previous()
public abstract int following(int offset)
offset - the offset to begin scanning. Valid values
are determined by the CharacterIterator passed to
setText(). Invalid values cause
an IllegalArgumentException to be thrown.
public int preceding(int offset)
offset - the offset to begin scanning. Valid values are
determined by the CharacterIterator passed to setText().
Invalid values cause an IllegalArgumentException to be thrown.
public boolean isBoundary(int offset)
offset - the offset to check.
public abstract int current()
public abstract CharacterIterator getText()
public void setText(String newText)
newText - new text to scan.public abstract void setText(CharacterIterator newText)
newText - new text to scan.public static BreakIterator getWordInstance()
Locale.getDefault()public static BreakIterator getWordInstance(Locale where)
where - the local. If a specific WordBreak is not
avaliable for the specified locale, a default WordBreak is returned.
public static BreakIterator getLineInstance()
Locale.getDefault()public static BreakIterator getLineInstance(Locale where)
where - the local. If a specific LineBreak is not
avaliable for the specified locale, a default LineBreak is returned.
public static BreakIterator getCharacterInstance()
Locale.getDefault()public static BreakIterator getCharacterInstance(Locale where)
where - the local. If a specific character break is not
avaliable for the specified local, a default character break is returned.
public static BreakIterator getSentenceInstance()
Locale.getDefault()public static BreakIterator getSentenceInstance(Locale where)
where - the local. If a specific SentenceBreak is not
avaliable for the specified local, a default SentenceBreak is returned.
public static Locale[] getAvailableLocales()
get*Instance methods of this class can return
localized instances.
The array returned must contain at least a Locale
instance equal to Locale.US.
BreakIterator instances are available.
protected static long getLong(byte[] buf,
int offset)
protected static int getInt(byte[] buf,
int offset)
protected static short getShort(byte[] buf,
int offset)
Ces informations proviennent du site de http://java.sun.com
Le contenu de cette page provient du site de Sun, et est généré depuis un cache sur l'infobrol après certains traitements automatisés. La présentation peut donc différer du document original, mais le contenu aussi. Vous pouvez utiliser ce bouton pour afficher la page originale du site de Sun :
Maintenir les pages en cache sur différents sites peut offrir plus de disponibilité.
Chaque page est indexée dans la base de donnée, ce qui permet de retrouver facilement les informations, au moyen des sommaires, du moteur de recherche interne, etc.
Des facilités sont mises en place pour que les membres de l'infobrol puissent effectuer des traductions en français des différents documents. Ceci devrait permettre aux débutants en programmation Java de consulter les API en français s'ils maîtrisent moins bien la langue de Shakespeare. Dans le cas où une traduction a été soumise, elle est disponible au moyen d'un lien en bas de page. Si la traduction a été validée, la page s'affiche par défaut en français, et un lien en bas de page permet d'atteindre la version en anglais.
Le code sur l'infobrol est automatiquement coloré selon la syntaxe, et les différents mots clés sont transformés en liens pour accéder rapidement aux informations.
Vous avez la possibilité de partager vos expériences en proposant vos propres extraits de code en utilisant le bouton "ajouter un commentaire" en bas de page. Si vous visitez simplement l'infobrol, vous avez déjà accès à cette fonction, mais si vous étes membre du brol, vous pouvez en plus utiliser des boutons supplémentaires de mise en forme, dont la coloration automatique de vos extraits de codes.
Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.
5 mots clés dont 0 définis manuellement (plus d'information...).
Avertissement
Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.
Recherche (afficher)
Utilisateur (masquer)
Navigation (masquer)
Apparence (afficher)
Stats (afficher)
Citation (masquer)