Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top