-1

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.

1 Answers1

1

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!
Divyesh_08
  • 1,563
  • 15
  • 25