Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Saturday, August 7, 2021

How to String Split Example in Java - Tutorial

Java String Split Example
I don't know how many times I needed to Split a String in Java. Splitting a delimited String is a very common operation given various data sources e.g CSV file which contains input string in the form of large String separated by the comma. Splitting is necessary and Java API has great support for it. Java provides two convenience methods to split strings first within the java.lang.String class itself: split (regex) and other in java.util.StringTokenizer. Both are capable of splitting the string by any delimiter provided to them. Since String is final in Java every split-ed String is a new String in Java.

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.
If you feel enthusiastic about String and you can also learn some bits from those post.

If you like to read interview articles about String in Java you can see :

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
StringTokenizer stringtokenizer = new StringTokenizer(asseltClasses, ":");
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, the most reputed author in the Java programming world. I have read this book twice and can say it's 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

My personal favorite is String.split () because it’s defined in String class itself and its capability to handle regular expression which gives you immense power to split the string on any delimiter you ever need. Though it’s worth to remember following points about split method in Java

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 “|”
String assetTrading = "Gold Trading|Stocks Trading
              |Fixed Income Trading|Commodity Trading|FX trading";

String[] splits = assetTrading.split("\\|");  
// two \\ is required because "\"     itself require escaping

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";

String[] smartPhonesSplits = smartPhones.split("\\.");

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 of 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 a limit. You can further see these core Java courses to learn more about the advanced splitting of String in Java. 

//string split example with limit

String places = "London.Switzerland.Europe.Australia";
String[] placeSplits = places.split("\\.",2);

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 APIs.

Related Java tutorials


Anonymous said...

string split in Java is quite easy as you shown in your example. difficult part is to understand regular expression which used in split string method.

Jirka Pinkas said...

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

Also if you don't know much about regular expressions, you can use StringUtils class from project Apache Commons Lang. There's a split method, where delimiter is not regular expression, but ordinary String. This class also has more split methods everybody should know about like splitByCharacterType().

Anonymous said...

per the javadoc for 1.6: "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

Saifuddin Merchant said...

@Javin - I wouldn't recommend the regular expression version split of the String class - in most cases its not what you need.

It also works out pretty badly when you writing a library like function - e.g. split a string and create a map,
e.g a:A|b:B --> Map(){key=a, value=A}{key=b, value=B} now if your allowing the user a customized delimiter - he needs to know that '|' would be interpreted as a regular expression and pass "\\|" instead of just a simple char.

Regular expression to split a string is mostly an overkill!

Anonymous said...

Split String Examples in Java not a rocket science. String split examples are the one who beginners done in Java School or in first few weeks of Java training. If you know how does Split String method in Java is implemented or how Split String process works then please share.

Nathaniel Stowe said...

Compiler says:

1 error found:
File: F:\Dr.Java\SplitString\ [line: 4]
Error: Syntax error on tokens, delete these tokens

Javin @difference between Runnable and Thread in Java said...

Hi Nathaniel, check out for extra '}' ')', could be simple typo or else post your whole java program or relevant section for split string.

SENTHIL said...

One mistake in first example,

for(String asset: assetClasses){

Should use splits instead of assetClasses

timhoustontx said...

warning, we had a lot of string.split in our code and it was a slow point. Better to use apache StringUtils.split()

Anonymous said...

instead of using . or , use two special chars like $$ or ## and try to split the string. It wont happen and throws ArrayIndexOutOfBoundsException...

sivanagakrishna said...

hi javin how the split method internally perform the split operation on the expression

Rohan said...

There is one mistake in first example.It may be typo, but its bit confusing for beginners:

for(String asset: assetClasses){

In above example there should be splits instead of assetClasses

Ajay said...

But if give limit as 3 the split method is not working.please help me with that.

Anonymous said...

In below example there should be splits instead of assetClasses

for(String asset: assetClasses){

Anonymous said...

string str="aryan------is--a----android---------developer";
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.

Unknown 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.

Unknown said...

I want split this sentence:
India is No1 Country.

Output should be ,
1 India
2 is
3 No1
4 Country

Post a Comment