English 中文(简体)
将长字符串用合适的单词环绕换行
原标题:Break a long string into lines with proper word wrapping
  • 时间:2012-05-22 12:48:48
  •  标签:
  • java
  • regex
 String original = "This is a sentence.Rajesh want to test the application for the word split.";
 List matchList = new ArrayList();
 Pattern regex = Pattern.compile(".{1,10}(?:\s|$)", Pattern.DOTALL);
 Matcher regexMatcher = regex.matcher(original);
 while (regexMatcher.find()) {
     matchList.add(regexMatcher.group());
 }
 System.out.println("Match List "+matchList);

我需要将文字解析成一系列行, 长度不超过10个字符, 并且不会在行末出现字断字 。

我在我的假想中使用了低于逻辑的逻辑,但问题在于,如果在行尾出现间断,则在10个字符之后将问题解析到最接近的白色空间。

例如: 实际的句子是“ < enger> this is a mental. Rajesh 想要测试单词分割的应用程序 。 。” 但在逻辑执行后, 它的步调如下 。

匹配列表 [这是一个, ince. Rajesh, 想要, 测试, 应用, 用于, 单词, 拆分 。 ]

问题回答

所以,我设法做了以下工作, 最大行长度为 10, 但也把超过 10 的单词分割开来!

String original = "This is a sentence. Rajesh want to test the applications for the word split handling.";
List matchList = new ArrayList();
Pattern regex = Pattern.compile("(.{1,10}(?:\s|$))|(.{0,10})", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(original);
while (regexMatcher.find()) {
  matchList.add(regexMatcher.group());
}
System.out.println("Match List "+matchList);

其结果是:

This is a 
sentence. 
Rajesh want 
to test 
the 
applicatio
ns word 
split 
handling.

这个问题在某个时候被贴上格罗维的标签。假设格罗维的回答仍然有效,而你并不担心保护多个白色空间(例如:):

def splitIntoLines(text, maxLineSize) {
    def words = text.split(/s+/)
    def lines = [  ]
    words.each { word ->
        def lastLine = (lines[-1] +     + word).trim()
        if (lastLine.size() <= maxLineSize)
            // Change last line.
            lines[-1] = lastLine
        else
            // Add word as new line.
            lines << word
    }
    lines
}

// Tests...
def original = "This is a sentence. Rajesh want to test the application for the word split."

assert splitIntoLines(original, 10) == [
    "This is a",
    "sentence.",
    "Rajesh",
    "want to",
    "test the",
    "application",
    "for the",
    "word",
    "split."
]
assert splitIntoLines(original, 20) == [
    "This is a sentence.",
    "Rajesh want to test",
    "the application for",
    "the word split."
]
assert splitIntoLines(original, original.size()) == [original]

我避开了正正负值, 重不拉动。 这个代码单字行, 如果一个单词超过 10 个字符, 可以打破它 。 它也需要处理多余的空白 。

import static java.lang.Character.isWhitespace;

public static void main(String[] args) {
  final String original =
    "This is a sentence.Rajesh want to test the application for the word split.";
  final StringBuilder b = new StringBuilder(original.trim());
  final List<String> matchList = new ArrayList<String>();
  while (true) {
    b.delete(0, indexOfFirstNonWsChar(b));
    if (b.length() == 0) break;
    final int splitAt = lastIndexOfWsBeforeIndex(b, 10);
    matchList.add(b.substring(0, splitAt).trim());
    b.delete(0, splitAt);
  }
  System.out.println("Match List "+matchList);
}
static int lastIndexOfWsBeforeIndex(CharSequence s, int i) {
  if (s.length() <= i) return s.length();
  for (int j = i; j > 0; j--) if (isWhitespace(s.charAt(j-1))) return j;
  return i;
}
static int indexOfFirstNonWsChar(CharSequence s) {
  for (int i = 0; i < s.length(); i++) if (!isWhitespace(s.charAt(i))) return i;
  return s.length();
}

打印 :

Match List [This is a, sentence.R, ajesh, want to, test the, applicatio, n for the, word, split.]




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签