In this section, we will be showing on how to split string value into tokens in java. String object has a split() method. This method can take an argument a regex pattern. Here is a sample java code that makes use of split method to tokenize a given string.

package com.javatutorialhq.tutorial;

public class SplitString {

	/**
	 * This java sample code shows how to split
	 * String value into tokens using
	 * split method under String class
	 * Property of teknoscope.com
	 * All Rights Reserved
	 * Version 1.0
	 * 05/27/2012
	 */
	public static void main(String[] args) {
		String input = "01 02 03 04656 TEST";
		System.out.println("String to tokenize:"+input);
		String[] tokens = input.split(" ");
		for(int var = 0; var !=tokens.length; var++){
			System.out.println("token"+var+":"+tokens[var]);
		}
	}
}

Running the script above will give you the following output:

String to tokenize:01 02 03 04656 TEST
token0:01
token1:02
token2:03
token3:04656
token4:TEST

The input string is “01 02 03 04656 TEST” and has been assigned to variable input which in turn to be of type String. The regex pattern that we use is a space. The tokenize string is of type String[] thus the for loop to access the value and display the tokenize string into our console.

Suggested Reading List: