Java - Split String by Space
Last Updated :
09 Dec, 2024
In Java, to split a string by spaces, we can use the split()
method of the
String
class, StringTokenizer
, or Java 8 Streams. In this article, we will learn the usage of these methods with examples to split the string by spaces in Java.
Example: The simplest and most commonly used way to split a string by spaces is by using the split()
method of the String
class.
Java
// Java Program to Split a String by Space
public class SplitString {
public static void main(String[] args) {
String s = "Java is a Programming Language.";
String[] w = s.split(" "); // Split by space
for (String word : w) {
System.out.println(word);
}
}
}
OutputJava
is
a
Programming
Language.
Other Methods to Split a String by Space
1. Using split()
with Multiple Spaces
If the string contains multiple spaces between words, we can handle it with a regular expression.
Java
// Java Program to Split a String by Space
public class SplitByString {
public static void main(String[] args) {
String s = "Java is a Programming Language.";
String[] w = s.split("\\s+"); // Split by one or more spaces
for (String word : w) {
System.out.println(word);
}
}
}
OutputJava
is
a
Programming
Language.
Explanation:
In the above example, the
split("\\s+")
matches one or more whitespace characters, including spaces, tabs, and newlines.
2. Using StringTokenizer
We can use the StringTokenizer
class for splitting strings into tokens based on a delimiter, such as a space. This method is considered outdated but still works.
Java
// Java Program to Split a String by Space
// using StringTokenizer
import java.util.StringTokenizer;
public class SplitString {
public static void main(String[] args) {
String s = "Java is a programming language";
StringTokenizer t = new StringTokenizer(s, " "); // Tokenize by space
while (t.hasMoreTokens()) {
System.out.println(t.nextToken());
}
}
}
OutputJava
is
a
programming
language
Explanation: In the above example, the StringTokenizer
splits the string into tokens based on the space character. The hasMoreTokens()
method checks if there are more tokens, and nextToken()
returns the next word.
3. Using Streams (Java 8+)
In Java 8, we can use the Streams for a more functional approach, especially if we need additional processing.
Java
// Java Program to Split a String by Space
// using Streams
import java.util.Arrays;
public class SplitString {
public static void main(String[] args) {
String s = "Java is a programming language";
// Use Stream to split and process the words
Arrays.stream(s.split(" ")) // Split the string by space
.forEach(System.out::println);
}
}
OutputJava
is
a
programming
language
Explanation:In the above example, the split(" ")
method splits the string into words, and Arrays.stream()
converts the array to a stream. The forEach()
method is used to print each word in the stream.
Similar Reads
Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re
6 min read
Java String substring() Method In Java, the substring() method of the String class returns a substring from the given string. This method is most useful when you deal with text manipulation, parsing, or data extraction.This method either takes 1 parameter or 2 parameters, i.e., start and end value as arguments.In case the end par
2 min read
Java String trim() Method The trim() method of the String class in Java is used to remove leading and trailing whitespace from a string. The Unicode value of the space character is "\u0020". It does not remove any whitespace in the middle of the string. This method returns a new string with the whitespace removed and leaves
4 min read
How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim
7 min read
Splitter Class | Guava | Java Guava's Splitter Class provides various methods to handle splitting operations on string, objects, etc. It extracts non-overlapping substrings from an input string, typically by recognizing appearances of a separator sequence. This separator can be specified as a single character, fixed string, regu
2 min read