2014-03-31 83 views
0

我需要在每三个字(使用JAVA)中分割一个字符串。 例如:如何在每三个字中分割一个字符串(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);  
} 
+0

如果最后一行('我need')真的是在结果? – Keppil

+0

是的,最后一行是必需的。 – MariaH

+1

那么为什么单个“需要”不在结果中呢? –

回答

3

用人几乎为你做同样的逻辑,这里是解决方案:

​​3210

输出:

This is an 
is an example 
an example of 
example of what 
of what I 
what I need. 
I need. 
need. 

但如果你需要下面的outp UT只有

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

好的,它的工作原理!我真的很感激你的帮助=) – MariaH

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

但是这会导致: 这是我需要的\ n示例。这不是我正在寻找的输出。 – MariaH

+0

对不起,我想你误解了我。我在说这个解决方案将导致三行:第1行:这是一个; 2号线:什么的例子;第3行:我需要。请看看我的问题的描述,你会发现这不是正确的输出。 – MariaH

+0

啊,我很抱歉。我误解了你。让我进行编辑。 – tambykojak

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. 
+0

感谢您的帮助。这个也可以工作! – MariaH

0

试试这个,这个工程

代码 进口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"); 
     } 
    } 
} 
+0

我也试过这个,但是我得到了最后一行的java.lang.ArrayIndexOutOfBoundsException。 – MariaH

+0

将发生java.lang.ArrayIndexOutOfBoundsException,如果您尝试访问大于其大小的数组,则上面的代码完全适合我。你能粘贴你尝试过的代码吗? –

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

} 

}