0

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!

1
  • to ensure that I can split all the strings -- you'll always be able to split your file. If your delimiter is not present, you'll just have a big chunk but it's no problem. What is your problem exactly? Commented May 6, 2014 at 9:56

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for me this makes the most sense and it works in my code
1

Probably you want to iterate over your String array. You can do it like that:

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.