Various ways to split String in Java – 3 ways

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

Way 1.A: Using StringTokenizer class

  • 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 2nd example (Way 1.B)

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

 

Way 1.B: Using StringTokenizer class by specifying a delimiter

  • We can also split a String on the basis of DOT
  • 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
  • as 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 below examples (Way 2.A & 2.B)

 

Way 2.A: Using split() method of String class (using space as 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

 

Way 2.B: Using split() method of String class (using dot {.} as 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 as 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

 

Way 3.A: Using split() method of StringUtils class from Apache with space as delimiter

  • split() method of StringUtils class takes 2-arguments with 1st argument as string to be split and 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";

		// use split() method of StringUtils class with space as 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

 

Way 3.B: Using split() method of StringUtils class from Apache with DOT as delimiter

  • split() method of StringUtils class takes 2-arguments with 1st argument as string to be split and 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";

		// use split() method of StringUtils class with DOT as 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.

 

Happy Coding !!
Happy Learning !!

Java: String intern() method
Various ways to reverse a String in Java – 4 ways