How to Split a String by Space in Java
-
Split a String Using the
split()
Method in Java -
Split a String Using the
split()
andtrim()
Methods in Java -
Split a String Using the
split()
Method in Java -
Split a String Using the
StringTokenizer
Class in Java -
Split a String Using the
split()
andcompile()
Methods in Java -
Split a String Using the
split()
Method in Java
This tutorial introduces how to split a string by space in Java.
There are several ways to split a string in Java, such as the split()
method of the String
class, the split()
method of the StringUtils
class, the StringTokenizer
class, the compile()
method of Pattern
, etc.
Split a String Using the split()
Method in Java
Java provides a method split()
to split a string based on the specified char. It is String
class method, and it returns an array of string after spiting the string. We can further access each string from the array by using its index value.
We use regex in the split()
method to split the string by whitespace. See the example below.
public class SimpleTesting {
public static void main(String[] args) {
String str = "Hello This is DelfStack";
String[] newStr = str.split("\\s+");
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
Output:
Hello
This
is
DelfStack
Split a String Using the split()
and trim()
Methods in Java
If the string contains whitespace at the start of the string, it will return an array containing the first index empty. To avoid this problem, we can use the trim()
method of the String
class to trim all the leading and trailing spaces from the string and then apply the split()
method to get an array of all the result string.
Since it returns an array, we should use the for
loop to traverse all its elements index-wise. See the below example.
public class SimpleTesting {
public static void main(String[] args) {
String str = " Hello This is DelfStack";
str = str.trim();
String[] newStr = str.split("\\s+");
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
Output:
Hello
This
is
DelfStack
Split a String Using the split()
Method in Java
Apart from Java String
class, there is another class, StringUtils
, that belongs to the Apache
library. So, if you are working with the Apache
commons library, you can use this class and its split()
method to split the string by whitespace.
This split()
method does not take regex as an argument; it requires a string argument that needs to be split. See the below example.
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting {
public static void main(String[] args) {
String str = "Hello This is DelfStack";
String[] newStr = StringUtils.split(str);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
Output:
Hello
This
is
DelfStack
Split a String Using the StringTokenizer
Class in Java
We can use the StringTokenizer
class to split a string by whitespace. It returns the token as string after splitting. See the example below.
import java.util.StringTokenizer;
public class SimpleTesting {
public static void main(String[] args) {
String str = "Hello This is DelfStack";
StringTokenizer tokens = new StringTokenizer(str, " ");
String[] newStr = new String[tokens.countTokens()];
int index = 0;
while (tokens.hasMoreTokens()) {
newStr[index] = tokens.nextToken();
System.out.println(newStr[index]);
index++;
}
}
}
Output:
Hello
This
is
DelfStack
Split a String Using the split()
and compile()
Methods in Java
The compile()
method belongs to the Pattern
class, and the split()
method then can be used to get an array of the split string. We use the compile()
method to specify the split char. See the example below.
import java.util.regex.Pattern;
public class SimpleTesting {
public static void main(String[] args) {
String str = "Hello This is DelfStack";
final Pattern space = Pattern.compile(" ");
String[] newStr = space.split(str);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
Output:
Hello
This
is
DelfStack
Split a String Using the split()
Method in Java
The split()
method of the String
class can be used to split a string on the specified index. For example, if we want to split only the first three whitespaces, we can simply pass this number to the method as the second argument. See the example below.
public class SimpleTesting {
public static void main(String[] args) {
String str = "Hello This is DelfStack";
String[] newStr = str.split(" ", 3);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
Output:
Hello
This
is DelfStack