【问题标题】:How to split a string in every three words (JAVA) [duplicate]如何在每三个单词中拆分一个字符串(JAVA)[重复]
【发布时间】:2023-03-28 07:45:02
【问题描述】:

我需要在每三个单词中拆分一个字符串(使用 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);    
}

【问题讨论】:

  • 最后一行 (I need) 真的应该在结果中吗?
  • 是的,需要最后一行。
  • 那为什么结果中没有单一的“需要”?
  • 最简单的更改是,将条件更改为len - 1,并在循环中添加一个特殊情况if (i == len -1) { print str[i] + " " + str[i+1] }
  • split 方法返回字符串数组。 String splited[] = phrase.split(" ");

标签: java string split


【解决方案1】:

这应该可以!

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

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

【讨论】:

  • 但这会导致:这是我需要的 \n 示例。这不是我要寻找的输出。
  • 对不起,我想你误解了我的意思。我是说这个解决方案将产生三行: Line#1: This is an;第 2 行:示例;第 3 行:我需要。请看一下我的问题的描述,你会发现这不是正确的输出。
  • 啊,我很抱歉。我确实误会了你。让我编辑一下。
  • 现在可以正常使用了。谢谢您的帮助!我只是不需要最后一个词,但这是我的问题的一个特例。而且修改代码也很简单:for(int i = 0; i &lt; words.length-1; i ++) 不包括最后一个字。
【解决方案2】:

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

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

【讨论】:

  • 好吧,它有效!非常感谢您的帮助 =)
【解决方案3】:
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.

【讨论】:

  • 感谢您的帮助。这个也可以!
【解决方案4】:

试试这个,效果很好

代码 导入 java.io.; 导入 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");
        }
    }
 }

【讨论】:

  • 我也试过这个,但最后一行出现 java.lang.ArrayIndexOutOfBoundsException。
  • java.lang.ArrayIndexOutOfBoundsException 将发生,如果您尝试访问大于其大小的数组,上面的代码对我来说非常有效。你能粘贴你试过的代码吗?
【解决方案5】:

下面的程序会将三个单词的组合存储在一行中,以便您使用它

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

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 2019-12-08
    • 2016-11-07
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多