简体   繁体   English

数组拆分字符串java

[英]arrays splitting strings java

I'm sure this is a fairly easy question for someone but I cant work out the best way to do it as a relative beginner. 我敢肯定,对于某人来说,这是一个相当简单的问题,但是作为一个相对初学者,我无法找到最佳方法。

I am splitting a large file (the string temp) into about a 100 strings and setting it as an array, but I don't know the exact number of strings. 我正在将一个大文件(字符串temp)拆分为大约100个字符串并将其设置为数组,但是我不知道确切的字符串数。

String[] idf = temp.split("===========");

String class1 = idf[0];
String class2 = idf[1];
String class3 = idf[1];
etc etc..

What is the best way to ensure that I can split all the strings and store them in an array? 确保我可以拆分所有字符串并将它们存储在数组中的最佳方法是什么?

Any suggestions or pointers would be most appreciated thanks! 任何建议或指示,将不胜感激,谢谢!

Probably you want to iterate over your String array. 可能您想遍历String数组。 You can do it like that: 您可以这样做:

for(String s : idf) {
  //operate on s here
}

Use for-each to get elements from array. 使用for-each从数组中获取元素。
Please look at oracle official site for for-each loop. 请查看oracle官方站点的for-each循环。
Consider below code. 考虑下面的代码。

String tempString = "";
String regex = "";
String[] temparray = tempString.split(regex);
for (String temp : temparray)
{
    System.out.println(temp);
}

You can do it like this: 您可以这样做:

String list = "hey there how are you";
String[] strarray = list.split("\\s+");
for (String str: strarray)
{
    System.out.print(str);
}

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

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