Open In App

Java - Split String by Comma

Last Updated : 09 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, splitting a string by a comma is commonly achieved using the split() method of the String class. This method takes a regular expression as an argument and returns an array of substrings split at each match of the regex. Also, we can use other methods such as StringBuilder, StringTokenizer, or Java Streams to split a string by comma in Java.

Example 1: Here, we are using the split() method that uses a regex to split the string and returns an array of substrings.

Java
// Java program to split a string by a comma
public class SplitString {
  
    public static void main(String[] args) {
      
        String s = "apple,banana,cherry";
        String[] p = s.split(","); // Split using a comma
        for (String part : p) {
            System.out.println(part);
        }
    }
}

Output
apple
banana
cherry

Example 2: We can also use the StringBuilder for manually processes each character to split the string that offers more control.

Java
// Java program to split a string using StringBuilder
public class SplitString {
  
    public static void main(String[] args) {
      
        String s = "apple,banana,cherry";
        StringBuilder sb = new StringBuilder();
        for (char ch : s.toCharArray()) {
          
            // Detect comma to split
            if (ch == ',') {    
                System.out.println(sb);
                sb.setLength(0);   // Reset builder
            } else {
                sb.append(ch); // Add character
            }
        }
        System.out.println(sb); 
    }
}

Output
apple
banana
cherry

Example 3: We can also use the StringTokenizer class for splitting strings by a specific character. This method is considered outdated but still works.

Java
// Java program to split a string using StringTokenizer
import java.util.StringTokenizer;

public class SplitString {
  
    public static void main(String[] args) {
      
        String s = "apple,banana,cherry";
      
        // Comma as delimiter
        StringTokenizer t = new StringTokenizer(s, ","); 
        while (t.hasMoreTokens()) {
            System.out.println(t.nextToken());  // Retrieve token
        }
    }
}

Output
apple
banana
cherry

Example 4: We can also use Java Streams for a more functional approach, especially when performing additional operations after splitting the string.

Java
// Java program to split a string using Streams
import java.util.Arrays;

public class SplitString {
  
    public static void main(String[] args) {
      
        String s = "apple,banana,cherry";
      
        // Split and create a stream
        Arrays.stream(s.split(",")) 
              .forEach(System.out::println); 
    }
}

Output
apple
banana
cherry

When to Use Which Method

  • split() Method: This is simple and most commonly used.
  • StringBuilder: This offers granular control over splitting.
  • StringTokenizer: This is a legacy approach, less preferred in modern Java.
  • Streams: This is ideal for functional-style processing (Java 8+).

Next Article
Practice Tags :

Similar Reads