HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java split string – Java tokenize string examples

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 !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Darvin

    March 10, 2017

    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,

    • Swapnil Solunke

      February 8, 2018

      Hi Darvin,

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

      Thanks.
      Swapnil Solunke.

  2. fateme

    May 10, 2016

    thanks for the article, It helped me so much. TNQ 🙂

  3. Sarveswaran M

    March 9, 2015

    Nice & terse article.
    Thanks for the effort.
    Keep it up!!

  4. Moiz

    June 2, 2014

    Hi Lokesh,

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

    Regards,
    Moiz

    • Lokesh Gupta

      June 2, 2014

      Thanks Moiz for reading and pointing out. I corrected it.

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)