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:

Split string by comma in java

Example 2: Limit the number of substrings after split

In the above example, we are getting all the substrings after split operation. Let’s see how can we limit the number of substrings we get.

To do this, we will use the following variation of the string method. This allows us to pass the integer number along with the delimiter into the split method.

public String split(String regex, int limit)

Let’s see an example to understand how this limit affect the output of the program:

public class JavaExample
{
  public static void main(String[] args)
  {
    String str = "This,is,a,test,string   ";

    //passing comma as delimiter and a limit of 3
    String[] strArray = str.split(",", 3);

    for(String s: strArray){
      System.out.println(s);
    }
  }
}  

Output: As you can see, the number of substrings returned by the split method is limited to 3, this is because we passed the limit as 3.

Example 3: Split User entered String by comma delimiter

In the above examples, the string is initialized in the program. In this program, the string is entered by user, which we will capture using Scanner class.

import java.util.Scanner;
public class JavaExample
{
  public static void main(String[] args)
  {
    String str;

    //getting the string from user
    System.out.println("Enter a string that contains commas: ");
    Scanner scan = new Scanner(System.in);
    str = scan.nextLine();

    String[] strArray = str.split(",");

    System.out.println("Split result: ");
    for(String s: strArray){
      System.out.println(s);
    }
  }
}  

Output: User entered a string that contains commas, the program split this string using comma as delimiter and returned a string array. The elements of this string array are displayed using enhanced for loop.

Split String by Comma
❮ Java StringSplit String by Newline ❯

Top Related Articles:

  1. Java String substring() Method with examples
  2. Convert Comma Separated String to HashSet in Java
  3. How to Split a String in Java with Delimiter
  4. Split String by Newline in Java
  5. When to use ArrayList and LinkedList in Java

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap