Java String split() Method

Java String Split

Well, as the name of the method suggests, this method is going to split the string, over some given regular expression. This method returns the array of the strings split over the given regular expression. Here is an example, where we split the string over space. So every time we find a space, we split the string and put it in the array. Have a look at the program –

import java.util.Arrays;
public class StringMethods {
public static void main(String[] args) {
String exampleString = “We are learning java string methods from GyaniPandit”;
String[] stringarray = exampleString.split(” “);
System.out.println(Arrays.toString(stringarray));
}
}

Well, if you are finding something weird in the above program, it is the Arrays class. Since we are getting an array of strings, and we right now want to print it, that is why we are using the toString method. We are going to have a talk about arrays. You can explore it right now if you want to but just wanted to mention that we are going to explore arrays later. Here, you can find that the output comes out to be an array, with the strings separated at the spaces.