如何在每三个单词中分割一个字符串(JAVA)

时间:2014-03-31 04:28:16

标签: 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 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);    
}

5 个答案:

答案 0 :(得分:3)

这应该有效!

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

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

然后像这样打印:

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");
}

答案 1 :(得分:3)

采用与您相同的逻辑,这是解决方案:

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.
need.

但如果只需要以下输出

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);
    }

答案 2 :(得分:1)

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.

答案 3 :(得分:0)

试试这个,这是有效的

代码     import java.io. ;     import java.util。;

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");
        }
    }
 }

答案 4 :(得分:0)

程序下面的

将组合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);
    }

}

}