使用 .split 在 Java 中拆分字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19431710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting an array of strings in Java using .split
提问by S12
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!
但是,这给了我一个错误。我切换了一些东西,但每次它要么只打印线条,要么在某个点停止。我在 main 中唯一拥有的是与此相关的方法调用,这不是问题。如果有人有任何见解,将不胜感激。谢谢!
EDIT:
编辑:
"Count" in my code is the number of lines in the file I am reading. 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.
例如,这仅打印
The fat cat was black
然后结束。我试图围绕它构建另一个循环(如我的代码所示),但它最终只打印了 3 次“The fat cat was black”,而不是移动到文件的下一行。
回答by trevor-e
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.
有了这个,您也不需要计数变量。
回答by Brandon Johnson
it could be an error with your for loop stopping at count instead of (array.length || count)
您的 for 循环停止在 count 而不是 (array.length || count) 可能是错误
回答by Marcus
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. 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.
字符串数组 arr 包含来自文件的字符串,计数是从文件中取出的行数,因此它是将通过 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();
}
}
回答by sivaBE35
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']
代表空格、制表符、换行符。