0

I have comma separated values like below

String s1 = "a, b, c, d, e";
String s2 = "a, , , d, ";
String s3 = "a, b, c, d, ";

I need to split this string with ",". But the number of values should be 5. I mean the last empty value should present in the output array. Is there any solution for this.

Gopinath
  • 121
  • 1
  • 2
  • 10
  • you could use the `split` method from `String` – user85421 Mar 15 '19 at 22:34
  • *the last empty value should present in the output array* it will be present. As long as there are 4 commas at each line, split() will create an array of 5 items. – forpas Mar 15 '19 at 22:41
  • @forpas `System.out.println("2,3,4,5,".split(",").length);` returns 4. From the docs for `split(String regex)`: *Trailing empty strings are therefore not included in the resulting array.* – GBlodgett Mar 15 '19 at 22:44
  • @GBlodgett The OP's strings all contain a space after the comma. This is why he will get 5 items. – forpas Mar 15 '19 at 22:46
  • You actually can't use regex to parse CSV. It requires a parser. I suggest the Apache Commons one: https://commons.apache.org/proper/commons-csv/ – markspace Mar 15 '19 at 22:48

2 Answers2

1

I used String.split(String regex) method and see the Array has 5 elements. I hope this answers your query.

package net.javapedia.StringSplitExample;

public class Main {

    public static void main (String[] s) {

        String s1 = "a, b, c, d, e";
        String s2 = "a, , , d, ";
        String s3 = "a, b, c, d, ";
        // This is the String that issue is reported
        String s4 = "a,b,c,d,";
        String s5 = "a,b,c,d,";

        String[] s1Array= s1.split(",");
        String[] s2Array= s2.split(",");
        String[] s3Array= s3.split(",");
        //Below split ignore empty string
        String[] s4Array= s4.split(",");
        //Below split doesn't
        String[] s5Array= s5.split(",",-1);

        System.out.println(s1Array.length);
        System.out.println(s2Array.length);
        System.out.println(s3Array.length);
        //Prints 4
        System.out.println(s4Array.length);
        //Prints 5 :)
        System.out.println(s5Array.length);
    }

}

Output:

enter image description here

Reference

javapedia.net
  • 2,531
  • 4
  • 25
  • 50
  • Thanks for pointing it out. After fixing that I am able to see 4 chars. I did some analysis and found a good reference in stack overflow itself. Updating the answer with the same. Hope this helps. Kindly let me know if it doesn't. Thanks – javapedia.net Mar 16 '19 at 00:48
0

You could just use the split method :

String[] S2 = s2.split(",");

To prove it works ->

System.out.println(S2.length);
for (String str : S2) {
     if (str.equals(" ")) {
         System.out.println("Space");
     } else {
         System.out.println(str);
}
Ram K
  • 1,746
  • 2
  • 14
  • 23
  • not sure why someone would downvote this answer. It answers the OP's specific posted question and also provides valid proof. Its a different thing if the OP didn't have an empty space as the last string. But that's not true in this case. – Ram K Mar 15 '19 at 22:43