How to String Split Example in Java - Tutorial

Java String Split Example

split string example in JavaIn this article we will see how to split a string in Java both by using String’s split() method and StringTokenizer. If you have any doubt on anything related to split string please let me know and I will try to help.

Since String is one of the most common Class in Java and we always need to do something with String; I have created a lot of how to do with String examples e.g.


String Split Example Java

Let's see an example of splitting the string in Java by using the split() method of java.lang.String class:

//split string example in Java
String assetClasses = "Gold:Stocks:Fixed Income:Commodity:Interest Rates";
String[] splits = asseltClasses.split(":");

System.out.println("splits.size: " + splits.length);

for(String asset: splits){

splits.size: 5
Fixed Income
Interest Rates

In above example, we have provided delimiter or separator as “:” to split function which expects a regular expression and used to split the string. When you pass a character as a regular expression than regex engine will only match that character, provided it's not a special character e.g. dot(.), star(*), question mark(?) etc.

You can use the same logic to split a comma separated String in Java or any other delimiter except the dot(.) and pipe(|) which requires special handling and described in the next paragraph or more detailed in this article. 

Now let see another example of split using StringTokenizer

// String split example using StringTokenizer
while (stringtokenizer.hasMoreElements()) {

Fixed Income
Interest Rates

You can further see this post to learn more about StringTokenizer. It's a legacy class, so I don't advise you to use it while writing new code, but if you are one of those developers, who is maintaining legacy code, it's imperative for you to know more about StringTokenizer.

You can also take a look at Core Java Volume 1 - Fundamentals by Cay S. Horstmann, of the most reputed author in Java programming world. I have read this book twice and can say its one of the best in the market to learn subtle details of Java.

Java String Split Example

How to Split Strings in Java – 2 Examples


1) Some special characters need to be escaped while using them as delimiters or separators e.g. "." and "|".  Both dot and pipe are special characters on a regular expression and that's why they need to be escaped. It becomes really tricky to split String on the dot if you don't know this details and often face the issue described in this article.

//string split on special character “|”


for(String trading: splits){

Gold Trading
Stocks Trading
Fixed Income Trading
Commodity Trading
FX trading

// split string on “.”
String smartPhones = "Apple IPhone.HTC Evo3D.Nokia N9.LG Optimus.Sony Xperia.Samsung Charge";


for(String smartPhone: smartPhonesSplits){

Apple IPhone
Nokia N9
LG Optimus
Sony Xperia
Samsung Charge

2) You can control a number of splitting by using overloaded version split (regex, limit). If you give a limit as 2 it will only create two strings. For example, in the following example, we could have a total of 4 splits but if we just wanted to create 2, to achieve that we have used limit. You can further see Core Java for the Impatient by Cay S. Horstmann to learn more about the advanced splitting of String in Java. 

//string split example with limit

String places = "London.Switzerland.Europe.Australia";

System.out.println("placeSplits.size: " + placeSplits.length );

for(String contents: placeSplits){

placeSplits.size: 2

To conclude the topic StringTokenizer is the old way of tokenizing string and with the introduction of the split since JDK 1.4 its usage is discouraged. No matter what kind of project you work you often need to split a string in Java so better get familiar with these API.

Related Java tutorials


Anonymous said...


Jirka Pinkas said...

Agree with Anonymous. Hardest part about String.split is regular expression.


Anonymous said...


Saifuddin.Merchant said...



Regular expression to split a string is mostly an overkill!

Anonymous said...


Nathaniel Stowe said...

Compiler says:

1 error found:
Error: Syntax error on tokens, delete these tokens



SENTHIL said...

One mistake in first example,

for(String asset: assetClasses){

Should use splits instead of assetClasses

timhoustontx said...


Anonymous said...


sivanagakrishna said...


Rohan Pathak said...


for(String asset: assetClasses){

In above example there should be splits instead of assetClasses

Ajay said...


Anonymous said...

In below example there should be splits instead of assetClasses

for(String asset: assetClasses){

Anonymous said...

then how we can remove this space except one between them.

Balaji said...

For more than one character such as $$ or ## as token, use escape characters for each character such as \\$\\$ , \\#\\#

sj said...

I want to split string in xml format based on a tag. Sample xml is




My task is to split above xml into string array with

I could not achieve this fucntionality using SAX/DOM/XPath etc.
I guess String.split() will be the only option for me. Can you suggest any alternative

Anonymous said...

@sj, your input is not particular formatted, but given that you are talking about parsing XML, I suggest you use XML parser like SAX or DOM. String.split() is not for that purpose.

Deepak Singh said...

sir your explanation procedure is nice.anyone can understand easly

Bimal Modak said...

Your first example program is wrong. Please correct that.

Javin Paul said...

Hello Guys, I have corrected the first example of program. IT should be the array (splits) instead of String (assetClasses). Thanks for pointing that out and really sorry for taking it so long to correct it.

Post a Comment