Java Program to split a String from Comma

I decided to solve this pickle when I noticed someone doing manual work in a sheet where there were thousands of products placed in a horizontal line separated by commas. The requirement was to place each product in a vertical column without the commas.

Yeah! That sounded fun to sort if the person didn’t complaint with every copy paste act. That was like every second. So I decided to help the person by using some simple code.

Java Program to split a String from Comma image meme

Luckily for us coders when we see tedious manual tasks, we are already forming simple programs in our head to make the work execute with the one click of a button.

So to that person’s relief I was able to do that, and she was able to get the job done within 5 seconds.

I wondered if I could help the rest of the world too to tackle such challenges, so decided to write a Java Program to split a String from Comma tutorial. Hope it is useful.

Java Program to split a String from Comma

In order to proceed with the tutorial for java program to split a string from comma, we need to have a String with multiple commas.

We will take an example of the following String to do that. Let us make our dream cricket team.

"Sachin Tendulkar, Virender Sehwag, Rahul Dravid, Saurav Ganguly, Virat Kohli, Rohit Sharma, Mahendra Singh Dhoni, Ravindra Jadeja, Hardik Pandya, Zaheer Khan, Jaspreet Bumrah"

Steps on How to Split a String from Comma

I am hoping that you have already opened your favourite IDE and are ready to start writing a small program.

The first step would be to, of course, to create a class and everything. I mean, duh, that’s the basic.

Step 1: Create a class. Give it a name. Write public static void main to get started.

public class stringProgram {
   public static void main(String[] args) {
   }
}

Step 2: Then we have to put the above mentioned String in a variable. Let us do that:

public class stringProgram {
   public static void main(String[] args) {
       String cricketTeam = "Sachin Tendulkar, Virender Sehwag, Rahul Dravid, Saurav Ganguly, Virat Kohli, Rohit Sharma, Mahendra Singh Dhoni, Ravindra Jadeja, Hardik Pandya, Zaheer Khan, Jaspreet Bumrah";
   }
}

Step 3: In order to separate these names from their commas, we can make use of the split function of String.

cricketTeam.split(",");

NOTE: If you want to separate them by space, just replace the “,” with ” “.

Step 4: Let us put that in an array and then we can println the array variable.

String[] names = cricketTeam.split(",");

Time to print them so that they come one by one on a new line. We will use System.out.println for that. We will use a for loop to print each index.

Step 5: So let us have that for loop and display the names:

for(int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}

So our whole code must look something like this now:

public class stringProgram {
   public static void main(String[] args) {
       String cricketTeam = "Sachin Tendulkar, Virender Sehwag, Rahul Dravid, Saurav Ganguly, Virat Kohli, Rohit Sharma, Mahendra Singh Dhoni, Ravindra Jadeja, Hardik Pandya, Zaheer Khan, Jaspreet Bumrah";
       String[] names = cricketTeam.split(",");
       for(int i = 0; i < names.length; i++) {
         System.out.println(names[i]);
       }
   }
}

Output:

Let us check the output:

Sachin Tendulkar
 Virender Sehwag
 Rahul Dravid
 Saurav Ganguly
 Virat Kohli
 Rohit Sharma
 Mahendra Singh Dhoni
 Ravindra Jadeja
 Hardik Pandya
 Zaheer Khan
 Jaspreet Bumrah

You can see some issue with the spaces. We can get rid of that using a simple trim() function. Let us put that here:

System.out.println(names[i].trim());

Let us run it once again:

Sachin Tendulkar
Virender Sehwag
Rahul Dravid
Saurav Ganguly
Virat Kohli
Rohit Sharma
Mahendra Singh Dhoni
Ravindra Jadeja
Hardik Pandya
Zaheer Khan
Jaspreet Bumrah

There! Much better.

Instead of using a the whole old variant of for loop, we can make use of the much fancy forEach as well.

Like this:

for(String e: names) {
   System.out.println(e.trim());
}

It just means “for each String e in names array”. I have always thought “for each” is a lot more easier and fun to use, even though the old for loop is something we have learnt right from the very beginning.

So the whole code would now become:

public class stringProgram {
   public static void main(String[] args) {
       String cricketTeam = "Sachin Tendulkar, Virender Sehwag, Rahul Dravid, Saurav Ganguly, Virat Kohli, Rohit Sharma, Mahendra Singh Dhoni, Ravindra Jadeja, Hardik Pandya, Zaheer Khan, Jaspreet Bumrah";
       String[] names = cricketTeam.split(",");
       for(String e: names) {
       System.out.println(e.trim());
      }
   }
}

See the difference?

Using a List

Alternatively, we can transform our program using a list too.

Let us create a list where we will store the cricketer names. Then try to print that out.

    List<String> names = Arrays.asList(cricketTeam.split(","));
    for(String e: names) {
        System.out.println(e.trim());
    }

To use a list, you might be required to import:

import java.util.List;

and to use Arrays, you might be required to import:

import java.util.Arrays;

So the whole program would look something like this:

import java.util.Arrays;
import java.util.List;
public class stringProgram {
   public static void main(String[] args) {
       String cricketTeam = "Sachin Tendulkar, Virender Sehwag, Rahul Dravid, Saurav Ganguly, Virat Kohli, Rohit Sharma, Mahendra Singh Dhoni, Ravindra Jadeja, Hardik Pandya, Zaheer Khan, Jaspreet Bumrah";
       List<String> names = Arrays.asList(cricketTeam.split(","));
       for(String e: names) {
         System.out.println(e.trim());
      }
   }
}

If you run the above Java Program to split a String from Comma, you will get the same result.

Now you can simply copy paste the names that were retrieved, or even better create an excel file and paste them in the first column? How does that sound?

Well go ahead that’s the homework exercise for you.

Don’t know how to get started?

Here’s a hint:

You can take the help from how to write data into excel sheet tutorial.

Scottshak

Poet. Author. Blogger. Screenwriter. Director. Editor. Software Engineer. Author of "Songs of a Ruin" and proud owner of four websites and two production houses. Also, one of the geekiest Test Automation Engineers based in Ahmedabad.

You may also like...

Leave a Reply