简体   繁体   中英

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.

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. You can do it like that:

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

Use for-each to get elements from array.
Please look at oracle official site for for-each loop.
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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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