breakiterator.html

来自「API資料大全」· HTML 代码 · 共 927 行 · 第 1/3 页

HTML
927
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd"><!--NewPage--><HTML><HEAD><!-- Generated by javadoc on Thu Apr 27 23:36:35 PDT 2000 --><TITLE>Java 2 Platform SE v1.3: Class  BreakIterator</TITLE><LINK REL ="stylesheet" TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style"></HEAD><BODY BGCOLOR="white"><!-- ========== START OF NAVBAR ========== --><A NAME="navbar_top"><!-- --></A><TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0"><TR><TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1"><A NAME="navbar_top_firstrow"><!-- --></A><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">  <TR ALIGN="center" VALIGN="top">  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../overview-summary.html"><FONT CLASS="NavBarFont1"><B>Overview</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-summary.html"><FONT CLASS="NavBarFont1"><B>Package</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> &nbsp;<FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="class-use/BreakIterator.html"><FONT CLASS="NavBarFont1"><B>Use</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="package-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../index-files/index-1.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A>&nbsp;</TD>  <TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1">    <A HREF="../../help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A>&nbsp;</TD>  </TR></TABLE></TD><TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM><b>Java<sup><font size=-2>TM</font></sup>&nbsp;2&nbsp;Platform<br>Std.&nbsp;Ed. v1.3</b></EM></TD></TR><TR><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">&nbsp;<A HREF="../../java/text/AttributedString.html"><B>PREV CLASS</B></A>&nbsp;&nbsp;<A HREF="../../java/text/ChoiceFormat.html"><B>NEXT CLASS</B></A></FONT></TD><TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">  <A HREF="../../index.html" TARGET="_top"><B>FRAMES</B></A>  &nbsp;&nbsp;<A HREF="BreakIterator.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD></TR><TR><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">  SUMMARY: &nbsp;INNER&nbsp;|&nbsp;<A HREF="#field_summary">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_summary">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD><TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">DETAIL: &nbsp;<A HREF="#field_detail">FIELD</A>&nbsp;|&nbsp;<A HREF="#constructor_detail">CONSTR</A>&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD></TR></TABLE><!-- =========== END OF NAVBAR =========== --><HR><!-- ======== START OF CLASS DATA ======== --><H2><FONT SIZE="-1">java.text</FONT><BR>Class  BreakIterator</H2><PRE><A HREF="../../java/lang/Object.html">java.lang.Object</A>  |  +--<B>java.text.BreakIterator</B></PRE><DL><DT><B>All Implemented Interfaces:</B> <DD><A HREF="../../java/lang/Cloneable.html">Cloneable</A></DD></DL><HR><DL><DT>public abstract class <B>BreakIterator</B><DT>extends <A HREF="../../java/lang/Object.html">Object</A><DT>implements <A HREF="../../java/lang/Cloneable.html">Cloneable</A></DL><P>The <code>BreakIterator</code> class implements methods for finding the location of boundaries in text. Instances of <code>BreakIterator</code> maintain a current position and scan over text returning the index of characters where boundaries occur. Internally, <code>BreakIterator</code> scans text using a <code>CharacterIterator</code>, and is thus able to scan text held by any object implementing that protocol. A <code>StringCharacterIterator</code> is used to scan <code>String</code> objects passed to <code>setText</code>. <p> You use the factory methods provided by this class to create instances of various types of break iterators. In particular, use <code>getWordIterator</code>, <code>getLineIterator</code>, <code>getSentenceIterator</code>, and <code>getCharacterIterator</code> to create <code>BreakIterator</code>s that perform word, line, sentence, and character boundary analysis respectively. A single <code>BreakIterator</code> 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. <p> Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. <p> Sentence boundary analysis allows selection with correct interpretation of periods within numbers and abbreviations, and trailing punctuation marks such as quotation marks and parentheses. <p> 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. <p> 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. <p> <code>BreakIterator</code> is intended for use with natural languages only. Do not use this class to tokenize a programming language. <P> <strong>Examples</strong>:<P> Creating and using text boundaries <blockquote> <pre> 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);      } } </pre> </blockquote> Print each element in order <blockquote> <pre> 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));     } } </pre> </blockquote> Print each element in reverse order <blockquote> <pre> 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));     } } </pre> </blockquote> Print first element <blockquote> <pre> public static void printFirst(BreakIterator boundary, String source) {     int start = boundary.first();     int end = boundary.next();     System.out.println(source.substring(start,end)); } </pre> </blockquote> Print last element <blockquote> <pre> public static void printLast(BreakIterator boundary, String source) {     int end = boundary.last();     int start = boundary.previous();     System.out.println(source.substring(start,end)); } </pre> </blockquote> Print the element at a specified position <blockquote> <pre> 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)); } </pre> </blockquote> Find the next word <blockquote> <pre> 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.charAt(p))                 return last;         }         last = current;         current = wb.next();     }     return BreakIterator.DONE; } </pre> (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><P><DL><DT><B>See Also: </B><DD><A HREF="../../java/text/CharacterIterator.html"><CODE>CharacterIterator</CODE></A></DL><HR><P><!-- ======== INNER CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><A NAME="field_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Field Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static&nbsp;int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/text/BreakIterator.html#DONE">DONE</A></B></CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DONE is returned by previous() and next() after all valid boundaries have been returned.</TD></TR></TABLE>&nbsp;<!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected </CODE></FONT></TD><TD><CODE><B><A HREF="../../java/text/BreakIterator.html#BreakIterator()">BreakIterator</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructor.</TD></TR></TABLE>&nbsp;<!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%"><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TD COLSPAN=2><FONT SIZE="+2"><B>Method Summary</B></FONT></TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="../../java/lang/Object.html">Object</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../java/text/BreakIterator.html#clone()">clone</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Create a copy of this iterator</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>abstract &nbsp;int</CODE></FONT></TD><TD><CODE><B><A HREF="../../java/text/BreakIterator.html#current()">current</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return character index of the text boundary that was most recently returned by next(), previous(), first(), or last()</TD>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?