Break a long string into lines with proper word portability

 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);

I need to parse text into an array of strings no longer than 10 characters long and should not be interrupted by a word at the end of a string.

I used the logic below in my script, but the problem is analyzing the nearest space after 10 characters if there is a gap at the end of the line

for example: Actual sentence: “ This is a sentence. Rajesh wants to test the application for separating words. ” But after executing the logic, getting it, as shown below.

List of matches [This, nce.Rajesh, want, test, apply, for a word, split.]

+3
source share
3 answers

, 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.
+4

- Groovy. , Groovy - , (, ''):

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]
+2

I avoided regex, as this does not pull weight. This is a wrapping codeword, and if one word is more than 10 characters, it violates it. He also takes care of extra spaces.

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();
}

Print

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

All Articles