How to String Split Example in Java - Tutorial (original) (raw)

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

OutPut Gold Stocks Fixed Income Commodity 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){ 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

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

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

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