Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何在Java中拆分一个句子_Java_Regex_String_Split - Fatal编程技术网

我如何在Java中拆分一个句子

我如何在Java中拆分一个句子,java,regex,string,split,Java,Regex,String,Split,我试图拆分正在打印到控制台上的一个句子,以避免像so@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? 有没有一种方法可以使用St

我试图拆分正在打印到控制台上的一个句子,以避免像so@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()

根据它的精确程度,可以很好地实现这一点。如果只需要在空格处拆分,可以使用一个简单的

String eg = "Example sentence with, some. spaces! and stuff";
String[] splitSentence = eg.split(" ");
将执行此操作,在每个空格处拆分字符串,从而将单词及其相邻的特殊字符作为字符串数组返回。然后,您可以简单地添加字符(中间有空格),如果您通过边框(在您的示例中为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;
}
String getConsoleFormattedString(字符串s,int rowLength){
字符串[]split=s.split(“”);
字符串ret=“”;
int计数器=0,
对于(int i=0;i如果(counter+split[i]+180个字母,为了简单起见,您可以这样做,根据行的当前大小决定是否执行新行:

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();
}
公共静态字符串getLines(字符串行){
String[]words=line.split(“”);
StringBuilder str=新的StringBuilder();
int size=0;
for(int i=0;iif(size+words[i].length()+1
split
不是这里的最佳选项,但是您可以将
Pattern
Matcher
类与此正则表达式一起使用

\\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?
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

但是假设你能面对很长的单词,而这些单词不应该分开,你可以加上

\\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?
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

您可以在打印时使用\n吗?我只想有一个方法,可以传递一个字符串,然后它为我过滤和调整。如果不清楚,我很抱歉。哦,谢谢!我没有找到那个!试试这个谢谢,有些人提出的算法非常棒