I have a problem in my Java code:
I want to split a string in parts an undefined amound of parts with different length. I want to split it whenever a space appears in the string.
I have a problem in my Java code:
I want to split a string in parts an undefined amound of parts with different length. I want to split it whenever a space appears in the string.
You can split string easily like this:
class HelloWorld {
public static void main(String[] args) {
String str = "Hey there welcome to java!";
String[] splited = str.trim().split("\\s+");
for (String word : splited)
{
System.out.println(word);
}
}
}
Output:
Hey
there
welcome
to
java!