Java - Split String by Comma Last Updated : 09 Dec, 2024 Summarize Comments Improve Suggest changes Share 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); } } } Outputapple 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); } } Outputapple 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 } } } Outputapple 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); } } Outputapple banana cherry When to Use Which Methodsplit() 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+). Comment More infoAdvertise with us Next Article Java StringJoiner Class J juhisrivastav Follow Improve Article Tags : Java Java-Strings Java-StringTokenizer Practice Tags : JavaJava-Strings Similar Reads Java String split() Method The String split() method in Java is used to split a string into an array of substrings based on matches of the given regular expression or specified delimiter. This method returns a string array containing the required substring.Example: Here, we will split a String having multiple delimiters or re 6 min read How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter. In C: // Splits str[] according to given delim 7 min read Splitter Class | Guava | Java Guava's Splitter Class provides various methods to handle splitting operations on string, objects, etc. It extracts non-overlapping substrings from an input string, typically by recognizing appearances of a separator sequence. This separator can be specified as a single character, fixed string, regu 2 min read Convert String into comma separated List in Java Given a String, the task is to convert it into comma separated List. Examples: Input: String = "Geeks For Geeks" Output: List = [Geeks, For, Geeks] Input: String = "G e e k s" Output: List = [G, e, e, k, s] Approach: This can be achieved by converting the String into String Array, and then creating 2 min read Java StringJoiner Class StringJoiner class in Java provides an efficient way to concatenate multiple strings with a defined delimiter(character), optional prefix, and suffix. This class is especially useful when constructing formatted strings dynamically.Example 1: Java// Demonstration of StringJoiner Class import java.uti 3 min read Java String join() with examples The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. :public stat 2 min read Like