Java教程是针对JDK 8编写的。本页面中描述的示例和实践不利用后续版本中引入的改进,并且可能使用不再可用的技术。
有关Java SE 9和后续版本中更新的语言功能的摘要,请参阅Java语言更改。
有关所有JDK版本的新功能、增强功能以及删除或弃用选项的信息,请参阅JDK发行说明。
格式化文本或执行换行的应用程序必须找到潜在的换行位置。您可以使用使用getLineInstance方法创建的BreakIterator来找到这些换行位置或边界:
BreakIterator lineIterator =
BreakIterator.getLineInstance(currentLocale);
这个BreakIterator确定字符串中可以在下一行继续的位置。BreakIterator检测到的位置是潜在的换行位置。在屏幕上显示的实际换行可能不同。
下面的两个示例使用BreakIteratorDemo.java的markBoundaries方法显示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."