In this guide, we will discuss how to split a string by comma (,). You can use Java String split() method to split a given string.
Example 1: Split String by Comma
Here, we have a comma separated string. To split this string into substring using comma as delimiter, we are passing the , symbol in the split() method.
public class JavaExample
{
public static void main(String[] args)
{
String str = "This,is,a,test,string";
//passing comma as delimiter
String[] strArray = str.split(",");
//displaying string array elements
for(String s: strArray){
System.out.println(s);
}
}
}
Output: