Split String Java Examples
The Java language provides many ways to split a string. This article will look into the various ways of splitting a string in Java, including the use of a StringTokenizer, Scanner, and the split() method. Each of these methods have their own advantages and disadvantages, which will be discussed in detail. The code examples provided in this article will demonstrate how to use each of these methods to split a string in Java.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
**Algorithm:**
1. Take the string to be split as an input.
2. Using the String class in Java, use the split() method to split the string into an array of strings.
3. Iterate through the array and print out each word in the string.
**Sample Code:**
```Java
String str = "Split this string into separate words";
String[] words = str.split("\\s+");
for (String word : words) {
System.out.println(word);
}
```
Output:
Split
this
string
into
separate
words