Java – How to split a string

To split a string, uses String.split(regex). Review the following examples :


	String phone = "012-3456789";
	String[] output = phone.split("-");
	System.out.println(output[0]);
	System.out.println(output[1]);

Output


	012
	3456789
Note
This split (regex) takes a regex as an argument, remember to escape the regex special characters, like period/dot.

1. Split a Period / dot

The period / dot is a special character in regex, you have to escape it either with a double backlash \\. or uses the Pattern.quote method.

TestSplit.java

package com.mkyong.test

import java.util.regex.Pattern;

public class TestSplit {

	public static void main(String[] args) {

		String test = "abc.def.123";
		String[] output = test.split("\\.");

		//alternative
		//String[] output = test.split(Pattern.quote("."));
		
		System.out.println(output[0]);
		System.out.println(output[1]);
		System.out.println(output[2]);
		
	}

}

Output


abc
def
123

Some common checking before split.

TestSplit.java

package com.mkyong.test

import java.util.regex.Pattern;

public class TestSplit {

	public static void main(String[] args) {

		String test = "abc.def.123";
		if(test.contains(".")){
			String[] output = test.split("\\.");
			if(output.length!=3){
				throw new IllegalArgumentException(test + " - invalid format!");
			}else{
				System.out.println(output[0]);
				System.out.println(output[1]);
				System.out.println(output[2]);
			}
		}else{
			throw new IllegalArgumentException(test + " - invalid format!");
		}
		
	}

}

2. StringTokenizer example

In the old days, Java developers like to use the StringTokenizer class to split a string. This is because the StringTokenizer class is available since JDK 1.0 and the String.split() is available since JDK 1.4

TestSplit.java

package com.mkyong.test

import java.util.StringTokenizer;

public class TestSplit {

	public static void main(String[] args) {

		String test = "abc.def.123";

		StringTokenizer token = new StringTokenizer(test, ".");

		while (token.hasMoreTokens()) {
			System.out.println(token.nextToken());
		}
		
	}

}

Output


abc
def
123
Note
This StringTokenizer is a legacy class, retained for compatibility reasons, the use is discouraged! Please use string.split().

References

  1. Java doc String.split
  2. Java StringTokenizer
  3. Java regular Expression
  4. Wikipedia : Regular Expression

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Greg StaleyIgorNagaEd Sanraj Recent comment authors
newest oldest most voted
Greg Staley
Guest
Greg Staley

excelent example…worked first time!

Igor
Guest
Igor

First one gives me ArrayOutOfBounds what shoud i do?

Naga
Guest
Naga

Excellent explanation …

raj
Guest
raj

String str=”117755449933662288″ ;
i want this to split and print it in the increment order and program has to be flexible

Ed San
Guest
Ed San

Hi raj, you can do that by writting nothing in the arguments of split String[] output = str.split(“”);

saas
Guest
saas

cool