Skip to content

Java String Split Example

Java string split example shows how to split string using the split method including by special characters like “.” (Dot), “|” (Pipe) etc. Also, how to limit the number of results returned by the split operation.

How to split a string in Java?

Use the split method of the String class to split a string in Java.

This method splits a string and returns an array of strings parts.

Output

How to the limit number of string parts returned by the split method?

Use the overloaded split method which accepts a limit parameter to limit the number of matches returned by the split method.

The limit parameter denotes the number of times the pattern is applied on a string and thus the length of the resulting array.

Output

Please note that the passing limit as 0 is the same as calling the split method without the limit parameter.

How to split a string using special characters?

It is no different from splitting a string using normal delimiters. However, since the split method accepts regular expression, if you want to split a string using a character that has a special meaning in the regular expression (called metacharacter), then those characters need to be escaped using the backslash.

Below given are the special metacharacters which need to be escaped.

a) Split a String by pipe “|”

Output

b) By caret “^”

c) By dollar “$”

d) By dot “.”

e) By a question mark “?”

f) By asterisk “*”

g) By plus “+”

h) By opening parenthesis “(“

i) By closing parenthesis “)”

j) By opening square bracket “[“

k) By opening curly bracket “{“

Note: Instead of escaping the special meaning characters using the double backslash, you can also use quote method of Pattern class which does that automatically for you.

How to split by a new line or line break?

If the string object contains multi-line text and you want to split a string by a new line or line break use the following code.

The above code takes care of both Unix and windows newline characters. Windows uses “\r\n” while Unix uses only “\n” as a line separator. Basically our pattern will split a string by zero or more “\r” followed by “\n” thus covering both Unix and Windows formats.

If you don’t want empty lines to be returned, change the pattern a bit as given below.

How to split a string using Apache Commons library?

Apache Commons is a very useful library that provides a lots of useful functionalities that are not included in standard Java. The StringUtils class provides a split method that can be used to split a String.

This method behaves like the split method of String class except that the separator argument is not a regular expression. Hence there is no need to escape special metacharacters while using this method.

Will print

How to return trailing empty strings with the split method?

Say for example you are trying to parse a CSV (comma separated values) file containing employee records. Here are the fields in the given sequence.

Out of these fields, Phone1 and Phone2 are optional fields. That means you may or you may not get Phone1/Phone2 values for any particular record.

Now consider this example where Phone1 and Phone2 values are not present for a particular record.

Output

The output is not what we expected. It should have printed the empty trailing string values for Phone1 and Phone2 fields even if they are empty. They are not printed because the split method without the limit argument internally calls the split method with the limit argument with limit value as 0. That, by default, does not return trailing empty string values.

To return trailing empty string values, use the split method with limit argument having value as -1 as given below.

Will now print

This example is a part of the Java String tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.