Friday, September 30, 2011

How to String Split Example in Java - Tutorial

Java String Split Example
I don't know how many times I need to Split String in Java. Split is very common operation given various data sources e.g CSV file which contains input string in form of large String separated by 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 to split 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 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 lot of How to with String e.g.
 How to replace String in Javaconvert String to Date in Java or convert String to Integer in Java. 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 string in Java by using split() function:

//split string example

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

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

for(String asset: assetClasses){
System.out.println(asset);
}

OutPut
splits.size: 5
Gold
Stocks
Fixed Income
Commodity
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.

Now let see another example of split using StringTokenizer

//string split example StringTokenizer

StringTokenizer stringtokenizer = new StringTokenizer(asseltClasses, ":");
while (stringtokenizer.hasMoreElements()) {
System.out.println(stringtokenizer.nextToken());
}

OutPut
Gold
Stocks
Fixed Income
Commodity
Interest Rates



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



 



//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){
System.out.println(trading);
}

OutPut:
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){
System.out.println(smartPhone);
}


OutPut:
Apple IPhone
HTC Evo3D
Nokia N9
LG Optimus
Sony Xperia
Samsung Charge


2) You can control number of split by using overloaded version split (regex, limit). If you give limit as 2 it will only creates two strings. For example in following example we could have total 4 splits but if we just want to create 2 we can use limit.

//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){
System.out.println(contents);
}

Output:
placeSplits.size: 2
London
Switzerland.Europe.Australia


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


Related Java tutorials

15 comments :

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

help?
Compiler says:

1 error found:
File: F:\Dr.Java\SplitString\SplitString.java [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){
System.out.println(asset);
}

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 Pathak said...

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

for(String asset: assetClasses){
System.out.println(asset);
}

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){
System.out.println(asset);
}

Anonymous said...

string str="aryan------is--a----android---------developer";
then how we can remove this space except one between them.

Post a Comment