BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Code to Split String by Comma

Last Updated: September 22, 2022 by Chaitanya Singh | Filed Under: java

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: