文档

Java™教程
隐藏目录
行边界
路径:国际化
课程:处理文本
部分:检测文本边界

行边界

格式化文本或执行换行的应用程序必须找到潜在的换行位置。您可以使用使用getLineInstance方法创建的BreakIterator来找到这些换行位置或边界:

BreakIterator lineIterator =
    BreakIterator.getLineInstance(currentLocale);

这个BreakIterator确定字符串中可以在下一行继续的位置。BreakIterator检测到的位置是潜在的换行位置。在屏幕上显示的实际换行可能不同。

下面的两个示例使用BreakIteratorDemo.javamarkBoundaries方法显示BreakIterator检测到的行边界。markBoundaries方法通过在目标字符串下方打印插入符(^)来表示行边界。

根据BreakIterator,行边界出现在一系列空格字符(空格、制表符、换行符)的终止之后。在下面的示例中,注意可以在检测到的任何边界处换行:

She stopped.  She said, "Hello there," and then went on.
^   ^         ^   ^     ^      ^     ^ ^   ^    ^    ^  ^

潜在的换行也会在连字符后立即发生:

There are twenty-four hours in a day.
^     ^   ^      ^    ^     ^  ^ ^   ^

下一个示例使用名为formatLines的方法将长字符串文本分成固定长度的行。该方法使用BreakIterator来定位潜在的换行位置。formatLines方法简短、简单,并且由于使用了BreakIterator,与语言环境无关。以下是源代码:

static void formatLines(
    String target, int maxLength,
    Locale currentLocale) {

    BreakIterator boundary = BreakIterator.
        getLineInstance(currentLocale);
    boundary.setText(target);
    int start = boundary.first();
    int end = boundary.next();
    int lineLength = 0;

    while (end != BreakIterator.DONE) {
        String word = target.substring(start,end);
        lineLength = lineLength + word.length();
        if (lineLength >= maxLength) {
            System.out.println();
            lineLength = word.length();
        }
        System.out.print(word);
        start = end;
        end = boundary.next();
    }
}

BreakIteratorDemo程序调用formatLines方法如下:

String moreText =
    "She said, \"Hello there,\" and then " +
    "went on down the street. When she stopped " +
    "to look at the fur coats in a shop + "
    "window, her dog growled. \"Sorry Jake,\" " +
    "she said. \"I didn't know you would take " +
    "it personally.\"";

formatLines(moreText, 30, currentLocale);

调用formatLines后的输出结果为:

She said, "Hello there," and
then went on down the
street. When she stopped to
look at the fur coats in a
shop window, her dog
growled. "Sorry Jake," she
said. "I didn't know you
would take it personally."

上一页:句子边界
下一页:将拉丁数字转换为其他Unicode数字