chevron_left
354 points
12 7 5

String handling is a significant feature in Java. String class offers an array of methods to work on strings.

One such method which can split a string based on a specific regular expression is split().

syntax:

split(String str) - returns a string array after splitting the given string based on matches of given regular expression.

Example:
String string="This article illustrate the splitting a string";

	String arr[]=string.split(" ");
	System.out.println("Displaying array \n" + Arrays.toString(arr));

The output of the program:
Displaying array
[This, article, illustrate, the, splitting, a, string]

In the example, we considered a space as delimiter to split the string. split() method returns an array of substrings which are stored in the string array arr.

StringTokenizer class in java allows to extract the tokens(substrings) based on the delimiter set.(multiple delimiters). As we know delimiters are the characters that separates the string. For example * : , ; , , | ,** et cetera.
StringTokenizer class can be instantiated as :

Syntax : StringTokenizer(String str, String delimiters)

Example:

String str="This:arti,cle:illustrate,the:splitting:a,string";
StringTokenizer st=new StringTokenizer(str,":,");

It accepts two arguments, first argument is the input string with delimiters and second argument is the delimiter string based on which the string is parsed and separated into tokens.

Code Snippet

package simple.examples;

import java.util.Arrays;
import java.util.StringTokenizer;

public class StringsSplit {

public static void main(String[] args) {
	
	String string="This:arti,cle:illustrate,the:splitting:a,string";
	StringTokenizer st=new StringTokenizer(string,":,");
	int i=1;
	while(st.hasMoreTokens())
	{			
		System.out.println("Token " + i + ":" +  st.nextToken());
		i++;
	}
}

}
output:
Token 1:This
Token 2:arti
Token 3:cle
Token 4:illustrate
Token 5:the
Token 6:splitting
Token 7:a
Token 8:string

In the preceding example, the string includes set of delimiters like ;:, . We begin with instantiating a StringTokenizer object st to parse and separate the tokens based only on " :," .

StringTokenizer class defines following methods to retrieve the tokens.
hasMoreTokens() - Tests if there are more tokens available from the string.
nextToken() - Returns the next token from this string tokenizer.

1 Comment

You have worked hard to understand the question and made it easy for us.Thanks a lot DivyaN.

More Posts

How to split a string in java with delimiter comma Sanjana Sagar - May 22
Make a string all lowercase in Java DivyaN - Aug 30
How to reverse a string in java without using reverse method Tania - Aug 16
How to count number of words in a string in java kesav.eee - Aug 12
Find a word in a string java usmanhashmi - Aug 11
Iterate over a string in java usmanhashmi - Aug 11
Calculate sum of all numbers present in a string java Hafsa_Rao - Jul 17
How to append a character to a string in Java mhfaisal - Jul 9
How to copy a string in java amna - Jun 26
Java program to find duplicate characters in a string Jeevani - Jun 10