java - how to split string in to multiple parts?

Posted by Ewen on Stack Overflow See other posts from Stack Overflow or by Ewen
Published on 2012-08-30T15:35:46Z Indexed on 2012/08/30 15:38 UTC
Read the original article Hit count: 219

Filed under:
|
|
|
|

I have a string that contains a value "firstword second third", and an ArrayList. I want to split the whole string in to by spaces and add the splitted strings in to the ArrayList.

For example,"firstword second third" can be split to three separate strings and added three times in to the ArrayList. "1 2 3 4" can be splitted in to 4 strings and added 4 times in to the ArrayList. See the code below:

public void separateAndAdd(String notseparated) {
for(int i=0;i<canBeSepartedinto(notseparated);i++{
//what should i put here in order to split the string via spaces?
thearray.add(separatedstring);
}
}

public int canBeSeparatedinto(String string) //what do i put here to find out the amount of spaces inside the string? return .... }

Please leave a comment if you dont get what I mean or I should fix some errors in this post. Thanks for your time!

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays