How do you split a String?
Given the following String
1 | String testString = "Hello world, I'm a test String!"; |
You can call the String.split
method directly on your String to split by spaces and returns a new String[]
Array
Split String
1 | String[] parts = testString.split(" "); |
We can now iterate the new Array
Iterate and Print parts Array
1 2 3 | for(int i=0; i<parts.length; i++) { System.out.println(parts[i]); } |
This will print out each part of the testString
Console Output
Hello
world,
I'm
a
test
String!