文本在指定长度后分割但不会使用grails中断单词

时间:2022-04-18 02:35:18

I have a long string that I need to parse into an array of strings that do not exceed 50 characters in length. The tricky part of this for me is making sure that the regex finds the last whitespace before 50 characters to make a clean break between strings since I don't want words cut off.

我有一个长字符串,我需要解析成一个长度不超过50个字符的字符串数组。对我来说,这个棘手的部分是确保正则表达式找到50个字符之前的最后一个空格,以便在字符串之间进行干净的中断,因为我不想要切断单词。

public List<String> splitInfoText(String msg) { 
     int MAX_WIDTH = 50; 
     def line = [] String[] words; 
     msg = msg.trim(); 
     words = msg.split(" "); 
     StringBuffer s = new StringBuffer(); 
     words.each {
        word -> s.append(word + " "); 
        if (s.length() > MAX_WIDTH) { 
          s.replace(s.length() - word.length()-1, s.length(), " "); 
          line << s.toString().trim();
          s = new StringBuffer(word + " "); 
        } 
     } 
     if (s.length() > 0) 
        line << s.toString().trim();
     return line; 
}

2 个解决方案

#1


6  

Try this:

尝试这个:

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile(".{1,50}(?:\\s|$)", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
}

#2


4  

I believe a Groovier version of Tim's answer is:

我相信Tim的答案的Groovier版本是:

List matchList = ( subjectString =~ /(?s)(.{1,50})(?:\s|$)/ ).collect { it[ 1 ] }

#1


6  

Try this:

尝试这个:

List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile(".{1,50}(?:\\s|$)", Pattern.DOTALL);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
    matchList.add(regexMatcher.group());
}

#2


4  

I believe a Groovier version of Tim's answer is:

我相信Tim的答案的Groovier版本是:

List matchList = ( subjectString =~ /(?s)(.{1,50})(?:\s|$)/ ).collect { it[ 1 ] }