I want to take in a String and split it every time I find a space. And then store each of these pieces into an array. Let's say I have this string:
String names = "amy bob lily harry luna james";
I also have this method declaration:
public static String[] seperateNames(String names) {
String[] newNames;
// Some code here
return newNames[];
}
What would I fill this method with so I can get something like this:
newNames = {"amy", "bob", "lily", "harry", "luna", "james"};
What I think I should do is create a for-loop and inside it have an if-statement that can check if there's space. But I really don't know how to go about doing this. I also think I will need to use trim() after everything is stored in an array to remove spaces before and after each name stored in the array.
Any help or advice appreciated. Thanks!