Java – Split String in 3 different ways based on delimiter

In this article, we will discuss various ways to split string using delimiter in Java i.e.;

Various ways to split string on the basis of delimiter:

  1. Using StringTokenizer class (not recommended)
  2. Using split() method of String class (with space & another with dot)
  3. Using Apache’s split() method of StringUtils class

Read String class in detail with example

Let us move forward and discuss all possible ways to split string in Java

1. StringTokenizer

  • Split String on the basis of space delimiter
  • Split String on the basis of DoT delimiter

1.1 Using StringTokenizer class and space delimiter

  • By default, String is split on the basis of space, if no delimiter is specified when StringToknizer class is used
  • Otherwise, we can specify delimiter as 2nd argument as explained in the next example

SplitBySpaceUsingStringTokenizer.java

package in.bench.resources.split.string;

import java.util.StringTokenizer;

public class SplitBySpaceUsingStringTokenizer {

	public static void main(String[] args) {

		// string content
		String testStr = "BenchResources.Net is committed "
				+ "to provide good Java tutorials";

		// use StringTokenizer to split string content
		StringTokenizer strTokens = new StringTokenizer(testStr);

		// print number of tokens in string content
		System.out.println("No. of tokens in string content : "
				+ strTokens.countTokens() + "\n");

		// iterate through tokens using while-loop
		while(strTokens.hasMoreTokens()) {

			// print token one-by-one iteratively
			System.out.println(strTokens.nextToken());
		}
	}
}

Output:

No. of tokens in string content : 8

BenchResources.Net
is
committed
to
provide
good
Java
tutorials

1.2 Using StringTokenizer class and DoT delimiter

  • We can also split a String on the basis of DOT delimiter
  • By specifying a delimiter as dot or period (.)
  • To specify DOT notation, use “.”

SplitByDotUsingStringTokenizer.java

package in.bench.resources.split.string;

import java.util.StringTokenizer;

public class SplitByDotUsingStringTokenizer {

	public static void main(String[] args) {

		// string content
		String testStr = "www.BenchResources.Net";

		// use StringTokenizer to split string content
		StringTokenizer strTokens =
				new StringTokenizer(testStr, ".");

		// print number of tokens in string content
		System.out.println("No. of tokens in string content : "
				+ strTokens.countTokens() + "\n");

		// iterate through tokens using while-loop
		while(strTokens.hasMoreTokens()) {

			// print token one-by-one iteratively
			System.out.println(strTokens.nextToken());
		}

		// token count after completion of iteration
		// print number of tokens in string content
		System.out.println("\nToken count after iteration : "
				+ strTokens.countTokens());
	}
}

Output:

No. of tokens in string content : 3

www
BenchResources
Net

Token count after iteration : 0

Note:

  • Although, we can any split string using StringTokenizer class but its usage is discouraged
  • It is kept in the JDK only for the purpose of backward compatibility
  • So, if anyone intended to split a string then it is encouraged to use split() method of String class as explained in the next set of examples

2. String.split() method of String class

  • Split String on the basis of space delimiter
  • Split String on the basis of DoT delimiter

2.1 Using split() method and space delimiter

  • split() method of String class takes 1-argument as delimiter
  • For this demo example, we will use delimiter as space
  • This can be either (“ “) or (“\\s”)
  • There is another variation available for split() method which takes 2-arguments, which allows us to define threshold limit as well

SplitStringWithSpaceAsDelimiter.java

package in.bench.resources.split.string;

public class SplitStringWithSpaceAsDelimiter {

	public static void main(String[] args) {

		// string content
		String testStr = "BenchResources.Net is committed "
				+ "to provide good Java tutorials";

		// use split() method of String class
		String[] strArray = testStr.split(" ");

		// print number of words in string content
		System.out.println("No. of words in string content : "
				+ strArray.length + "\n");

		// iterate using enhanced for-loop
		for(String word : strArray) {

			// print word one-by-one iteratively
			System.out.println(word);
		}
	}
}

Output:

No. of words in string content : 8

BenchResources.Net
is
committed
to
provide
good
Java
tutorials

2.2 Using split() method and DoT delimiter

  • split() method of String class takes 1-argument as delimiter
  • For this demo example, we will use delimiter as DOT or period
  • To define period or DOT, use notation (“\\.”);

SplitStringWithDotAsDelimiter.java

package in.bench.resources.split.string;

public class SplitStringWithDotAsDelimiter {

	public static void main(String[] args) {

		// string content
		String testStr = "www.BenchResources.Net";

		// use split() method of String class with DOT - delimiter
		String[] strArray = testStr.split("\\.");

		// print number of words in string content
		System.out.println("No. of words in string content : "
				+ strArray.length + "\n");

		// iterate using enhanced for-loop
		for(String word : strArray) {

			// print word one-by-one iteratively
			System.out.println(word);
		}
	}
}

Output:

No. of words in string content : 3

www
BenchResources
Net

3. StringUtils.split() method from Apache

  • Split String on the basis of space delimiter
  • Split String on the basis of DoT delimiter

3.1 Using Apache’s StringUtils.split() method and space delimiter

  • split() method of StringUtils class takes 2-arguments where,
    • 1st argument as string to be split
    • 2nd argument as delimiter
  • For this demo example, we will use delimiter as space
  • To define period or DOT, use notation (“\\s”);
  • Note: but if we don’t specify also, still it will be split string using space as delimiter
  • Because by default split() method of StringUtils class uses space as delimiter

StringUtilsSplitWithSpaceAsDelimiter.java

package in.bench.resources.split.string;

import org.apache.commons.lang.StringUtils;

public class StringUtilsSplitWithSpaceAsDelimiter {

	public static void main(String[] args) {

		// string content
		String testStr = "BenchResources.Net is committed "
				+ "to provide good Java tutorials";

		// split() method of StringUtils with space - delimiter
		String[] strArray = StringUtils.split(testStr);

		// print number of words in string content
		System.out.println("No. of words in string content : "
				+ strArray.length + "\n");

		// iterate using enhanced for-loop
		for(String word : strArray) {

			// print word one-by-one iteratively
			System.out.println(word);
		}
	}
}

Output:

No. of words in string content : 8

BenchResources.Net
is
committed
to
provide
good
Java
tutorials

3.2 Using Apache’s StringUtils.split() method and DoT delimiter

  • split() method of StringUtils class takes 2-arguments where
    • 1st argument as string to be split
    • 2nd argument as delimiter
  • For this demo example, we will use delimiter as DoT or period
  • To define period or DOT, use notation (“\\.”);
  • Note: by default split() method of StringUtils class uses space as delimiter

StringUtilsSplitWithDotAsDelimiter.java

package in.bench.resources.split.string;

import org.apache.commons.lang.StringUtils;

public class StringUtilsSplitWithDotAsDelimiter {

	public static void main(String[] args) {

		// string content
		String testStr = "www.BenchResources.Net";

		// split() method of StringUtils with DOT - delimiter
		String[] strArray = StringUtils.split(testStr, "\\.");

		// print number of words in string content
		System.out.println("No. of words in string content : "
				+ strArray.length + "\n");

		// iterate using enhanced for-loop
		for(String word : strArray) {

			// print word one-by-one iteratively
			System.out.println(word);
		}
	}
}

Output:

No. of words in string content : 3

www
BenchResources
Net

Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

Happy Coding !!
Happy Learning !!

Java - String intern() method
Java - 4 Ways to reverse a String