简体   繁体   English

使用.split在Java中拆分字符串数组

[英]Splitting an array of strings in Java using .split

I have browsed through questions similar to mine already on here, but have not found anything that will work for me. 我已经浏览了类似于我已经在这里的问题,但没有发现任何对我有用的东西。

Essentially what I have to do is read a file full of strings line by line, print them line by line and then word by word. 基本上我要做的是逐行读取一个完整的字符串文件,逐行打印然后逐字打印。 I would like to accomplish this using .split to create an array of each individual string. 我想用.split来创建每个单独字符串的数组。

For instance, my file reads: 例如,我的文件是:
The fat cat was black 肥猫是黑色的
The cow jumped over the moon 母牛跳过月亮
I came to say hello world 我来打招呼世界

etc. 等等

I have figured out how to read the file and print the file line by line, but I cannot figure out how to print it word by word. 我已经弄清楚如何读取文件并逐行打印文件,但我无法弄清楚如何逐字打印。 Each of these 3 are in their own functions, so I'm not sure if that plays any role in where something is placed. 这3个中的每一个都有它们自己的功能,所以我不确定它是否在放置某个东西的位置起作用。

This is what I have so far in my method: 这是我到目前为止在我的方法中所拥有的:

static public void printWords( String [ ] arr, int count )
{
    String [] s2 = arr[0].split( " " ); 

    for( int i = 0; i < count; i++)
    {
        System.out.println(  s2 [i] );
    }

    System.out.println();  
}

However, this is giving me an error. 但是,这给了我一个错误。 I switched around a couple things, but each time it prints either just the lines or stops at a certain point. 我换了几件东西,但每次只打印线或停在某一点。 The only thing I have in main is my method call pertaining to this, which is not the problem. 我唯一的主要是我的方法调用,这不是问题。 If anyone has any insight it would be greatly appreciated. 如果有人有任何见解,将不胜感激。 Thank you! 谢谢!

EDIT: 编辑:

"Count" in my code is the number of lines in the file I am reading. 我的代码中的“Count”是我正在阅读的文件中的行数。 So for this, it is 3. 所以对于这个,它是3。

I have tweaked the code to get what I need, however it only prints the first sentence. 我已经调整了代码以获得我需要的东西,但它只打印第一句。

String [] s2 = arr[0].split( " " );
    for (int j = 0 ; j < count ; j++)
    {

        for( int i = 3; i < s2.length; i++)
        {

        System.out.print(  s2 [i] + " " );
        }
    System.out.println();

    }

}

For example, this only prints 例如,这只打印
The fat cat was black 肥猫是黑色的

And then ends. 然后结束。 I am trying to construct another loop around it (as demonstrated by my code) but it ends up just printing "The fat cat was black" 3 times rather than moving to the next line of the file. 我正在尝试围绕它构建另一个循环(正如我的代码所示)但它最终只是打印“胖猫是黑色的”3次,而不是移动到文件的下一行。

Try something like this: 尝试这样的事情:

static public void printWords(String [ ] arr)
{

  for(String s : arr) {
      String[] s2 = s.split(" ");
      for(String results : s2) {
          System.out.println(results);
      }
  }

}

With this you don't need the count variable either. 有了这个,你也不需要计数变量。

你的for循环停止在count而不是(array.length || count)可能是一个错误

The count can only be used for the number of lines which make sense if this is a homework problem. 如果这是一个家庭作业问题,计数只能用于有意义的行数。 You were close to the solution but you had your split statement in the wrong place. 您接近解决方案但是您的split语句位于错误的位置。

The String array arr contains the strings from the file and the count is the number of lines taken the from the file so it is a limit for the iterator that will go through the arr array. String数组arr包含文件中的字符串,count是从文件中获取的行数,因此它是通过arr数组的迭代器的限制。 You had the right idea by using a seperate iterator to go through the s2 array as it only contained the words from a single line from arr. 你有一个正确的想法,通过使用一个单独的迭代器来通过s2数组,因为它只包含来自arr的单行中的单词。

Try this: 尝试这个:

static public void printWords( String [ ] arr, int count )
{
    for (int j = 0 ; j < count ; j++)
    {
        String [] s2 = arr[j].split( " " );
        for( int i = 0; i < s2.length; i++)
        {
            System.out.print(  s2 [i] + " " );
        }
        System.out.println();
    }
}

To specifically split by white space. 用白色空间专门划分。

public void split() {
            ArrayList<String> myArray=new ArrayList<>();
            myArray.add("The fat cat was black");
            myArray.add("The cow jumped over the moon");
            for (String strLine : myArray) {
                String[] strWord = strLine.split("[\\s']");
                for (String s : strWord) {
                    System.out.println(s);
                }
            }

        }

here [\\\\s'] represents a white space, tabs, line breaks. 这里[\\\\s']代表一个空格,制表符,换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM