Last Updated:

Example of using the split method in Java

The split(String regex) method is used to split a string into an array of strings.

The following is a program that demonstrates the use of split.

SplitStringIntoStringArray.java

split(String regex, int numOfStrings) is an overloaded function that splits an input string into a certain number of rows. We used a backslash to use special characters as regular characters as regular characters.

The result of the program:

Using the separator: [How, use, method, split]
Split into 2 lines: [How, use the split method]
split method with special delimiter: [How, use, method, split]

Let's look at another way to break a string into words just by using a regular expression. The following is a code listing that breaks the input string into words by spaces and commas:

The result of splitting with a regular expression:

Video tutorial with an example of using the split() method in Java.