fosho fosho - 6 months ago 12
Java Question

Split string into individual words Java

I would like to know how to split up a large string into a series of smaller strings or words.
For example:


I want to walk my dog.


I want to have a string:
"I"
,
another string:
"want"
, etc.

How would I do this?

Answer

Use split() method

Eg:

String s = "I want to walk my dog";

 String[] arr = s.split(" ");    

 for ( String ss : arr) {

       System.out.println(ss);
  }
Comments