如何每三个单词拆分一个字符串(JAVA)

如何每三个单词拆分一个字符串(JAVA),java,string,split,Java,String,Split,我需要每三个单词拆分一个字符串(使用JAVA)。 例如: "This is an example of what I need." 产出将是: This is an is an example an example of example of what of what I what I need I need 谢谢你的帮助 我试过这样做。 我不知道如何得到最后两个单词,或者当短语少于3个单词时该怎么做 String phrase = "This is an example of what I

我需要每三个单词拆分一个字符串(使用JAVA)。 例如:

"This is an example of what I need."
产出将是:

This is an
is an example
an example of
example of what
of what I
what I need
I need
谢谢你的帮助

我试过这样做。 我不知道如何得到最后两个单词,或者当短语少于3个单词时该怎么做

String phrase = "This is an example of what I need."
String[] splited = phrase.split(" ");

for (int i = 0; i < phraseSplited.length - 2; i++) {
      threeWords = splited[i] + " " + splited[i+1] + " " + splited[i+2];
      System.out.println(threeWords);    
}
String phrase=“这是我需要的示例。”
String[]splited=phrase.split(“”);
for(int i=0;i
这应该行得通

首先,将所有单词拆分为一个单独的单词数组。您可以这样做,因为您知道每个单词都被
(一个空格)除

然后像这样打印:

for(int i = 0; i < words.length; i ++) {
    System.out.print(words[i] + " ");

    if(i + 1 < words.length)
        System.out.print(words[i + 1] + " ");

    if(i + 2 < words.length)
        System.out.print(words[i + 2]);

    System.out.print("\n");
}
for(int i=0;i
采用与您几乎相同的逻辑,以下是解决方案:

String myString = "This is an example of what I need.";
        String[] words = myString.split("\\s+");
        for (int i = 0; i < words.length; i++) {
            String threeWords;
              if (i == words.length - 1)
                  threeWords = words[i]; 
              else if(i == words.length - 2)
                  threeWords = words[i] + " " + words[i + 1]; 
              else 
                  threeWords = words[i] + " " + words[i + 1] + " " + words[i + 2];
              System.out.println(threeWords);
    }
但如果您只需要以下输出

This is an
is an example
an example of
example of what
of what I
what I need.
I need.
稍微修改一下代码就可以了

for (int i = 0; i < words.length -1; i++) {
            String threeWords;
              if(i == words.length - 2)
                  threeWords = words[i] + " " + words[i + 1]; 
              else 
                  threeWords = words[i] + " " + words[i + 1] + " " + words[i + 2];
              System.out.println(threeWords);
    }
for(int i=0;i
试试这个,这个很管用

代码 导入java.io。; 导入java.util

公共类文件1{
公共静态void main(字符串[]args)
{
String word=“这是我需要的示例。”;
字符串[]wordArray=word.split(\\s+);
INTC;

对于(int i=0;i,下面的程序将把3个单词的组合存储在一行中,以便您可以使用它

 package com.loknath.lab;

 public class Demo {
public static void main(String[] args) {
    String se = "This is an example of what I need ";
    String temp[] = se.split(" ");
    String temp1[] = new String[temp.length / 3 + 1];

    int line = -1;
    for (int i = 0; i < temp.length; i++) {

        if (i % 3 == 0) {
            line += 1;
            temp1[line]="";
        }

        temp1[line] = temp1[line] + temp[i] + " ";
    }

    for (String stringBuffer : temp1) {
        System.out.println(stringBuffer);
    }

}
package com.loknath.lab;
公开课演示{
公共静态void main(字符串[]args){
String se=“这是我需要的一个示例”;
字符串温度[]=se.split(“”);
字符串temp1[]=新字符串[temp.length/3+1];
int行=-1;
对于(int i=0;i

}

最后一行(
I need
)真的应该在结果中吗?是的,最后一行是需要的。那么为什么结果中没有单个的“need”?最简单的更改是,将条件更改为
len-1
,并在循环中添加一个特例,
如果(I==len-1){print str[I]+“”+str[I+1]}
split方法返回字符串数组。
String splited[]=phrase.split(“”)
但这将导致:这是我需要的一个例子。这不是我想要的输出。对不起,我想你误解了我。我是说这个解决方案将导致三行:第1行:这是一个;第2行:什么的例子;第3行:我需要。请看一下我问题的描述,你会发现这不是正确的rect output.ahhh,很抱歉。我误解了您的意思。让我进行编辑。现在它可以正常工作了。谢谢您的帮助!我不需要最后一个单词,但这是我的问题的一个特殊情况。修改代码也很简单:
for(int I=0;I
排除了最后一个词。好吧,它很有效!我真的很感谢你的帮助=)我也尝试过这个,但最后一行出现了java.lang.ArrayIndexOutOfBoundsException。java.lang.ArrayIndexOutOfBoundsException会发生,如果你试图访问大于其大小的数组,上面的代码对我来说非常适合。你能粘贴你尝试过的代码吗?谢谢你的帮助。这个也可以!
for (int i = 0; i < words.length -1; i++) {
            String threeWords;
              if(i == words.length - 2)
                  threeWords = words[i] + " " + words[i + 1]; 
              else 
                  threeWords = words[i] + " " + words[i + 1] + " " + words[i + 2];
              System.out.println(threeWords);
    }
public static void main(String args[]) throws IOException {
        String phrase = "This is an example of what I need.";
        String splited[] = phrase.split(" ");
        String threeWords;

        for (int i = 0; i < splited.length -1; i++) {
            String a = splited[i];
            String b = splited[i+1];
            String c = null;
            if(i != splited.length-2) {
                c = splited[i+2];
            } else {
                c = "";
            }
            threeWords = a + " " + b + " " + c;
            System.out.println(threeWords);                
        }
    }
This is an
is an example
an example of
example of what
of what I
what I need.
I need.
public class file1 {

      public static void main(String[] args)
     {

        String word= "This is an example of what I need.";
        String[] wordArray = word.split("\\s+");
        int c;

        for(int i=0;i<wordArray.length; i++)
        {       
            c=i+2;
            for(int j=i;j<=c; j++)
            {
                System.out.println(wordArray[j]);
            }
            System.out.println("\n");
        }
    }
 }
 package com.loknath.lab;

 public class Demo {
public static void main(String[] args) {
    String se = "This is an example of what I need ";
    String temp[] = se.split(" ");
    String temp1[] = new String[temp.length / 3 + 1];

    int line = -1;
    for (int i = 0; i < temp.length; i++) {

        if (i % 3 == 0) {
            line += 1;
            temp1[line]="";
        }

        temp1[line] = temp1[line] + temp[i] + " ";
    }

    for (String stringBuffer : temp1) {
        System.out.println(stringBuffer);
    }

}