Java String Handling

Java String Split Example

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

How to split a String in Java?

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

This method splits a string and returns an array of result Strings.

Output

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

Use overloaded split method which accepts 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 String and thus length of the resulting array.

Output

Please note that, passing limit as 0 is 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 which has a special meaning in the regular expression (called metacharacter), than those characters need to be escaped using backslash.

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

a) Split a String by pipe “|”

Output

b) Split a String by caret “^”

c) Split a String by dollar “$”

d) Split a String by dot “.”

e) Split a String by question mark “?”

f) Split a String by asterisk “*”

g) Split a String by plus “+”

h) Split a String by opening parenthesis “(“

i) Split a String by closing parenthesis “)”

j) Split a String by opening square bracket “[“

k) Split a String by opening curly bracket “{“

Note:

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

How to split a String by new line or line break?

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

The above code takes care of both Unix and windows new line 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 which provides lot many useful functionality. StringUtils class provides split method which can be used to split a String.

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

Will print

How to return trailing empty Strings with split method?

Say for example you are trying to parse a CSV (comma separated values) file containing employee records. Here are the fields in 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

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 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 split method with limit argument having value as -1 as given below.

Will now print

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

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

About the author

rahimv

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.

Add Comment

Your email address will not be published. Required fields are marked *