Java split string – Java tokenize string examples

Split string into array is a very common task for Java programmers specially working on web applications. In web applications, many times we have to pass data in CSV format or separated based on some other separator such $,# or another character.

Before using this data further, it must be splitted to separate string tokens. We will learn to split string into arraylist or array in given examples.

Table of contents

1. StringTokenizer    
2. String.split()
3. StringUtils.split()       
4. Guava Splitter

1. Java String split – StringTokenizer

Using StringTokenizer to split string in java is really easy to use and has been in java from a long time.

1.1. Single delimiter

Java program to split string by space example.

String str = "I am sample string and will be tokenized on space";

StringTokenizer defaultTokenizer = new StringTokenizer(str);

System.out.println("Total number of tokens found : " + defaultTokenizer.countTokens());

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

System.out.println("Total number of tokens found : " + defaultTokenizer.countTokens());

Program output.

Total number of tokens found : 10
I
am
sample
string
and
will
be
tokenized
on
space
Total number of tokens found : 0

We saw that in starting the count of tokens was 10, and after fetching all tokens, count reduced to 0.

1.2. Multiple delimiters

This is real good usecase. It allows you to split string where delimiters can be more than one.

String url = "https://howtodoinjava.com/java-initerview-questions";

StringTokenizer multiTokenizer = new StringTokenizer(url, "://.-");

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

Program output.

https
howtodoinjava
com
java
initerview
questions
As java docs says, 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.

2. Java split string – String.split()

String.split() method is better and recommended than using StringTokenizer. Here tokens are returned in form of a string array which we are free to use as we wish.

Program to split a string in java with delimiter comma.

String[] tokens = "I,am ,Legend, , oh ,you ?".split(",");

for (String token : tokens)
{
	System.out.println(token);
}

Program output.

I
am 
Legend
      //Empty token
 oh   //Space in starting
you ?

Above code is really easy to use but as we see in output, it needs extra care while handling the tokens. It return the empty tokens and does not trim the tokens by default. You need to do these specific handling token by token basis.

3. String split – StringUtils.split()

StringUtils.split() is very much similar to above approach and it also returns the String[] as output. We need to deal with string array as we had to in previous code. Only benefit is the code is faster.

String[] tokens = StringUtils.split("I,am ,Legend, , oh ,you ?",",");

for (String token : tokens)
{
	System.out.println(token);
}

Program output.

I
am 
Legend
      //Empty token
 oh   //Space in starting
you ?

4. Split string – Guava Splitter

Splitter is best. It looks good while writing and re-usable also. You create a splitter and re-use it as many times as you want. So it helps in achieving uniform logic for splitter application, for similar use-cases.

Another benefit is that it also provided some useful methods while building the splitter itself which eliminates a lot of after work after creating the tokens itself as we saw in above examples.

To build a beautiful splitter, write code like this:

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

And Now use it anywhere in code as you like:

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

Iterable<String> tokens2 = niceCommaSplitter.split("I,am ,Legend, , oh ,you ?"); 
for(String token: tokens2){
 System.out.println(token);
}

Program output.

I
am
Legend
oh
you ?

For you reference, you can download Guava library from there project’s home project.

OR, you can directly include it as maven dependency.

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>17.0</version>
</dependency>

Share your thoughts if you have some other better solutions on this very specific problem of split string into array.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

6 thoughts on “Java split string – Java tokenize string examples”

  1. Hi!

    Good day!

    I’m new to java and I have case study about tokens. basically I want to read a file a text file the count the number of tokens and occurrence. for example I have a text file called test.txt and it contains the below data:

    if (sizePlus1++ == max) {
    if (max == min) {

    I wan to check the line how many tokens and occurrence. output must be ,

    if = 2
    ( = 2
    sizePlus = 1
    1 = 1
    ++ = 1
    == = 2
    max = 2
    ) = 2
    { = 2

    and result should be either ascending or descending order by key or by value.

    Your prompt reply will be very much appreciated.

    Thank you very much,

    Reply
    • Hi Darvin,

      Please provide Sample input data and expected Output, so that I can understand what exactly you want to do.

      Thanks.
      Swapnil Solunke.

      Reply
  2. Hi Lokesh,

    Just to point a correction. You have copied the same code from String.split() in StringUtils.split() section.

    Regards,
    Moiz

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.