Split a String in Java - 5 different ways

In this section, we will show you five different ways to split a String in Java based on the delimiter.

1. Using String.split()

The split method in Java splits a string into substrings using a delimiter that is specified using a regular expression.

1. This method returns an array of strings.

2. This method takes a regex string as a parameter.

Split a whitespace separated String

//Split by a whitespace
String input = "yellow black green orange";
String[] colors = input.split(" ");


Split a comma separated String

//Split by a comma
String input = "yellow,black,green,orange";
String[] colors = input.split(",");


Split a dot separated String

//Split by a dot
String input = "123.12.12.1";
String[] splitted = input.split("\\.");


Split and Trim a comma separated String

String input = " yellow,black ,green, orange";
String[] colors = input.trim().split("\\s*,\\s*");

trim() method removes all the leading and trailing spaces in the string and returns a String. split("\\s*,\\s*") handles the extra spaces around delimiter.


2. Using Java 8 Stream

Split a comma separated String using Java 8 Stream 
String input = " yellow,black ,green, orange";
String[] colors = Arrays.stream(input.split(","))
.map(String::trim)
.toArray(String[]::new);

Arrays.stream() method returns a Sequential Stream from the array passed as the parameter. Here we are passing input.split(",") as parameter, which is an String array. The split method in Java splits a string into substrings using a comma that is specified using a regular expression. trim() method removes all the leading and trailing spaces in the string and returns a String. Finally, we are converting a Stream to an array using Stream toArray() method.


3. Using StringTokenizer

Split a comma separated String using StringTokenizer.
String input = "yellow, black,green,orange";
StringTokenizer strings = new StringTokenizer(input, ",");
while(strings.hasMoreTokens()){
String substring = strings.nextToken().trim();
System.out.println(substring);
}

The StringTokenizer class of the java.util package is designed to decompose a string into its components. It Splits a string into parts using the string comma as a delimiter. nextToken() returns the next substring


4. Using Google Guava Splitter

First add Google Guava to your project: https://mvnrepository.com/artifact/com.google.guava/guava
String input = "yellow, black,green,orange";
List<String> splitted = Splitter.on(',')
.trimResults()
.splitToList(input);

on() method returns a splitter that uses the given single-character separator. trimResults() removes leading and trailing whitespace from each returned substring. splitToList() splits sequence into string and returns them as an immutable list.


5. Using Apache Commons StringUtils

First add Apache Commons to your project: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

String input = "yellow,black,green,orange";
String[] splitted = StringUtils.split(input, ',');

split(String str, char separator) method splits the input String into an array, we need to specify the separator.

 

Complete Example:

import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) {

//Example 1 - split by a whitespace
String input1 = "yellow black green orange";
String[] colors1 = input1.split(" ");

//Example 2 - split by a comma
String input2 = "yellow,black,green,orange";
String[] colors2 = input2.split(",");

//Example 3 - split by a dot
String input3 = "123.12.12.1";
String[] splitted1 = input3.split("\\.");

//Split and Trim a comma separated String
String input4 = " yellow,black ,green, orange";
String[] colors4 = input4.trim().split("\\s*,\\s*");

//Using Java 8 Stream
String input5 = " yellow,black ,green, orange";
String[] colors5 = Arrays.stream(input5.split(","))
.map(String::trim)
.toArray(String[]::new);

//Split a comma separated String using StringTokenizer
String input6 = "yellow, black,green,orange";
StringTokenizer strings = new StringTokenizer(input6, ",");
while(strings.hasMoreTokens()){
String substring = strings.nextToken().trim();
}

//Using Google Guava Splitter
String input7 = "yellow, black,green,orange";
List<String> splitted2 = Splitter.on(',')
.trimResults()
.splitToList(input7);

//Using Apache Commons StringUtils
String input = "yellow,black,green,orange";
String[] splitted3 = StringUtils.split(input, ',');

}
}


More Interesting topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete