1

I have a String and I want to split it by ","

If suppose I have a String like,

 String test = "aa,bb,cc"; 

now I can split it by,

String[] spl = test.split(",");

And the spl.length is 3

If suppose my String is

 String test = ",,,";

Here the splitted String length is 0. But my expected answer is 3. My test String is dynamaic value and it may varies like, Now think I have a String like

String test = ",aa,dd,,,,,ff,gg"

Now the splited array length is 4. But I expected answer is 9 And I need to split by "," and I need the aa position at spl[1] and dd position as spl[2] and ff position as spl[7]

Can someone give the suggestion about to solve this issue..

No_Rulz
  • 2,679
  • 1
  • 20
  • 33

3 Answers3

7

Use split() with -1 as limit

     public static void main(String[] args) {
    String test = ",,,";
    System.out.println(Arrays.toString(test.split(",", -1))); // adds leading and trailing empty Strings .
    // so effectively its like adding "" before , after and between each ","

    String test1 = "aa,bb,cc";
    System.out.println(Arrays.toString(test1.split(",",-1)));
}

O/P :

[, , , ] -- > Length =4
[aa, bb, cc]
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • `,,,` gives me 4 and not 3. – james Jul 28 '14 at 07:29
  • 1
    public static void main(String[] args) { String test = ",,,"; String[] spl = test.split(",", -1); System.out.println(spl.length); } – james Jul 28 '14 at 07:30
  • @TheLostMind the answer is 4 not 3 – No_Rulz Jul 28 '14 at 07:31
  • 1
    @No_Rulz When you perform a split operation on a subject containing the delimiter 3 times, the output will be 4 elements, not 3 elements. Your expected result for `",,,".split(",", -1);` should be 4. Look at your example for: `"aa,bb,cc"`, the delimiter only occurs twice and you expect 3 elements to be returned, why would you not expect 4 elements to return when you have the delimiter 3 times? – Dan Temple Jul 28 '14 at 07:38
  • @DanTemple yes I agree with you :) – No_Rulz Jul 28 '14 at 07:53
  • 2
    @all -My bad.. This does return 4(forgot that a trailing empty string will also be added when we split.).. - edited my answer. – TheLostMind Jul 28 '14 at 08:15
  • @TheLostMind after reading dantemple comment only I also recognize my mistake.. and thanks for your answer :) – No_Rulz Jul 28 '14 at 08:27
  • 2
    @TheLostMind Ok, but your answer is still right :) As Dan Temple said, when you have a string like `a,b,c` you have two commas, but after splitting the resulting array will hold three values. So, the OP should expect that `,,,` will be splitted to four values, not three, that would be wrong. – BackSlash Jul 28 '14 at 09:50
2

To get the behavior you want you can just replace "," by " ,":

String test = ",,";
test = test.replace(",", " ,");
System.out.println((test.split(",").length));
0

With the split() function, java separates a String by the Substring of your choice. If there is nothing between them, the field will not be null, it will just be skipped.

In other programming languages, you could come across something like this:

String example = ',,,'
String[] example2 = example.split(',')
print(example2.length())

This could also deliver 4. Because there are 4 spaces around the ',' chars:

1,2,3,4

PKlumpp
  • 4,913
  • 8
  • 36
  • 64