How to split a String by Space in Java
To split a string by space, we can use the String.split()
method by passing a regex \\s+
as an first argument in Java.
Here is an example.
import java.util.Arrays;
public class Main
{
public static void main(String[] args) {
String str = "You can see me";
String[] splited = str.split("\\s+");
System.out.println(Arrays.toString(splited));
}
}
Output:
[You, can, see, me]