Java 如何提取字符串中的1到3个单词?

Java 如何提取字符串中的1到3个单词?,java,regex,Java,Regex,如何从字符串中提取1到3个单词?每行最多3个字 例如: The Pacific Ocean is the largest of the Earth's oceanic divisions String[] sequences = {""}; int numberOfSequnces = (int)words.length/3; for(int i=0;i<words.length;i++) { String word=words[i];

如何从字符串中提取1到3个单词?每行最多3个字

例如:

The Pacific Ocean is the largest of the Earth's oceanic divisions
    String[] sequences = {""};
    int numberOfSequnces = (int)words.length/3;

    for(int i=0;i<words.length;i++)
    {
        String word=words[i];
        String sequnce=word;
        for(int m=i+1;m<numberOfSequnces;m++)
        {
            for(int g=m;g<3;g++)
            sequnce +=words[g];
        }

        sequences[i]=sequnce;
    }
输出:

The pacific Ocean
is the largest
of the Earth's
oceanic divisions

尝试:

The Pacific Ocean is the largest of the Earth's oceanic divisions
    String[] sequences = {""};
    int numberOfSequnces = (int)words.length/3;

    for(int i=0;i<words.length;i++)
    {
        String word=words[i];
        String sequnce=word;
        for(int m=i+1;m<numberOfSequnces;m++)
        {
            for(int g=m;g<3;g++)
            sequnce +=words[g];
        }

        sequences[i]=sequnce;
    }
String[]序列={'};
int numberOfSequnces=(int)words.length/3;
对于(int i=0;i您的“输出”实际上是什么意思?如果只是指在控制台上打印:

String[] words = line.split(" ");
for(int i = 0; i < words.length; i++) {
    System.out.print(words[i]);
    if ((i + 1) % 3 != 0) {
        System.out.print(" ");
    } else {
        System.out.println();
    }
}
String[]words=line.split(“”);
for(int i=0;i
您的“输出”实际上是什么意思?如果只是指在控制台上打印:

String[] words = line.split(" ");
for(int i = 0; i < words.length; i++) {
    System.out.print(words[i]);
    if ((i + 1) % 3 != 0) {
        System.out.print(" ");
    } else {
        System.out.println();
    }
}
String[]words=line.split(“”);
for(int i=0;i
这将准确打印您发布的输出

String line = "The Pacific Ocean is the largest of the "
    + "Earth's oceanic divisions"; // <-- The line.
String[] words = line.split(" ");
int count = 0; // <-- A word count.
for (int i = 0; i < words.length; i++) {
  String word = (words[i] != null) ? words[i]
      .trim() : ""; // <-- Ternary, check for null and consecutive spaces.
  if (word.length() > 0) { // <-- make sure there is a word.
    if (count > 0) { // <-- check the word count.
      if (count % 3 == 0) { // <-- Is it divisible by 3? Newline.
        System.out.println();
      } else { // <-- not divisible by 3, space.
        System.out.print(' ');
      }
    }
    count++; // <-- There is a word, add one to the count.
    System.out.print(word); // <-- print it.
  }
}
String line=“太平洋是世界上最大的海洋”

+“地球海洋分区”//这将准确打印您发布的输出

String line = "The Pacific Ocean is the largest of the "
    + "Earth's oceanic divisions"; // <-- The line.
String[] words = line.split(" ");
int count = 0; // <-- A word count.
for (int i = 0; i < words.length; i++) {
  String word = (words[i] != null) ? words[i]
      .trim() : ""; // <-- Ternary, check for null and consecutive spaces.
  if (word.length() > 0) { // <-- make sure there is a word.
    if (count > 0) { // <-- check the word count.
      if (count % 3 == 0) { // <-- Is it divisible by 3? Newline.
        System.out.println();
      } else { // <-- not divisible by 3, space.
        System.out.print(' ');
      }
    }
    count++; // <-- There is a word, add one to the count.
    System.out.print(word); // <-- print it.
  }
}
String line=“太平洋是世界上最大的海洋”

+“地球的海洋分区”//您可以使用regexp来实现:

String sentence = "The Pacific Ocean is the largest of the Earth's oceanic divisions";
sentence = sentence.replaceAll("([^\\p{javaWhitespace}]*\\p{javaWhitespace}){3}", "$0"+System.getProperty("line.separator"));
System.out.println(sentence);

您可以使用regexp执行以下操作:

String sentence = "The Pacific Ocean is the largest of the Earth's oceanic divisions";
sentence = sentence.replaceAll("([^\\p{javaWhitespace}]*\\p{javaWhitespace}){3}", "$0"+System.getProperty("line.separator"));
System.out.println(sentence);
使用此模式
/(\S+\S+\S+\S+\S+/g

并替换为
$1\n

使用此模式
/(\S+\S+\S+\S+\S+/g

并替换为
$1\n


你能发布你尝试过的内容和遇到的问题吗?你能发布你尝试过的内容和遇到的问题吗?是的。我想打印出来。这不是在第一行打印四个字吗?为什么不在括号中写
I%3!=2
?你的情况看起来太复杂了。@DavidWallace我又修复了它)我只是想
(I+1)%3!=0
更容易理解。嗯,这比一分钟前有所改进:-)是的。我想把它打印出来。这不是在第一行打印四个字吗?为什么不直接写
I%3!=2
在括号中?你的病情看起来太复杂了。@DavidWallace我又治好了。:)我只是想
(I+1)%3!=0
更容易理解。好吧,这比一分钟前有了改进:-)聪明的方法。这个模式还会应用于其他字符串吗?聪明的方法。该模式还会应用于任何其他字符串吗?