我将如何在Java中拆分句子

时间:2014-07-31 09:49:18

标签: java regex string split

我试图将正在打印的句子拆分到控制台,以避免像@ 80字符那样被切断:

Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave ad
venturer?

所以我希望它像这样打印

Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave 
adventurer?

有没有办法用String.split()执行此操作?

3 个答案:

答案 0 :(得分:0)

根据其准确程度,Regex可以做得非常好。如果你只需要在空格处拆分,那就简单了

String eg = "Example sentence with, some. spaces! and stuff";
String[] splitSentence = eg.split(" ");

将完成这项工作,在每个空格处拆分字符串,从而将其相邻特殊字符作为String数组返回。然后你可以简单地添加字符(中间有空格),如果你通过边框(在你的情况下为80),弹出最后一个单词并添加'\n'

String getConsoleFormattedString(String s, int rowLength) {
  String[] split = s.split(" ");
  String ret = "";
  int counter = 0,

  for(int i = 0; i < split.length; i++) {
    if(counter + split[i] + 1 <= 80)
      ret += split[i] + " ";
    else {
      ret += "\n";
      counter = 0;
      i--;
    }
  }

  return ret;
}

我会让你弄明白如何使用&gt;处理单词80个字母,为简单起见

答案 1 :(得分:0)

您可以执行以下操作,根据线路当前大小决定是否使用新线路:

public static String getLines(String line){

    String[] words = line.split(" ");
    StringBuilder str = new StringBuilder();
    int size = 0;
    for (int i = 0; i < words.length; i++) {
        if(size==0){
            str.append(words[i]);
        }
        if(size+words[i].length()+1<=80){
            str.append(" ").append(words[i]);
            size++;
        }else{
            str.append("\n").append(words[i]);
            size = 0;
        }
        size+=words[i].length();
    }
    return str.toString();
}

另一种不同的做法:

public static String getLines2(String line){
    StringBuilder str = new StringBuilder();
    int begin = 0;

    while(begin<line.length()){
        int lineEnd = Math.min(begin + 80, line.length());
        while(lineEnd<line.length()&&line.charAt(lineEnd)!= ' '){
            lineEnd--;
        }
        str.append(line.subSequence(begin, lineEnd));
        if(lineEnd<line.length()) str.append("\n");
        begin = lineEnd+1;
    }
    return str.toString();
}

答案 2 :(得分:0)

split不是最佳选择,但您可以将PatternMatcher类与此正则表达式一起使用

\\G.{1,80}(\\s+|$)

表示

  • \\G最后一场比赛的地方,或者如果它是第一次搜索匹配的迭代(所以还没有)字符串的开头(由^表示)
  • .{1,80}任何字符都可以出现一到八十次
  • (\\s+|$)一个或多个空格或字符串结尾

你可以这样使用

String data = "Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave "
        + "adventurer? ";
Pattern p = Pattern.compile("\\G.{1,80}(\\s+|$)");
Matcher m = p.matcher(data);
while(m.find())
    System.out.println(m.group().trim());

输出:

Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave
adventurer?

但假设您可以面对不应该分割的很长的单词,您可以添加

\\S{80,}

到你的正则表达式,也让它找到长度为80或更多的非空白字符串。

示例:

String data = "Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave "
        + "adventurer? foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar";

Pattern p = Pattern.compile("\\G.{1,80}(\\s+|$)|\\S{80,}");
Matcher m = p.matcher(data);
while (m.find())
    System.out.println(m.group().trim());

输出:

Welcome to fancy! A text based rpg. Perhaps you could tell us your name brave
adventurer?
foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar-foo-bar