7
String abc = "a,b,c,d,,,";
String[] arr = abc.split(",");
System.out.println(arr.length);

Output is 4. But obviously my expectation is 7. Here is my solution:

String abc = "a,b,c,d,,,";
abc += "\n";
String[] arr = abc.split(",");
System.out.println(arr.length);

Why does it happen? Anyone could give my a better solution?

macemers
  • 2,194
  • 6
  • 38
  • 55

5 Answers5

9

Use the alternative version of String#split() that takes two arguments to achieve this:

String abc = "a,b,c,d,,,";
String[] arr = abc.split(",", -1);
System.out.println(arr.length);

This prints

7

From the Javadoc linked above:

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

Keppil
  • 45,603
  • 8
  • 97
  • 119
4

You can use lookahead:

String abc = "a,b,c,d,,,";
String[] arr = abc.split("(?=,)");
System.out.println(arr.length); //7
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • This is not the case here, although it works, it complicates the case with regex - specific operator – endriu_l Apr 15 '14 at 10:21
  • What do you mean by that comment and how can simple lookahead becomes complicated? – anubhava Apr 15 '14 at 10:24
  • Answer is as simple as reading javadoc and adding parameter to `split` call, You don't need to refer to regex cookbook to get this done. – endriu_l Apr 15 '14 at 10:34
  • You seem to be day dreaming. Who referred a regex cookbook here? For unknowns there is no `split` without regex support in Java. – anubhava Apr 15 '14 at 10:40
  • You know that, I know that, most of Jdevs know that (at least I hope most do)...but not everyone, and using this solution requires to know not only most basic regex rules but also groups and lookahead operator – endriu_l Apr 15 '14 at 10:57
  • You can fight the battle with OP who tagged the question `regex`. A simple lookahead is understood by anyone with basic knowledge of regex and there are no regex groups here. You probably don't understand lookahead syntax. – anubhava Apr 15 '14 at 11:29
  • bottom line, is that Your solution does not work, as it appends the delimeter to each element, so please, stop boasting about it. – endriu_l Apr 15 '14 at 19:34
  • Bottom line is it is beyond your understanding, it is for community to see what answer works and what doesn't. – anubhava Apr 15 '14 at 19:38
1

Use:

String[] arr = abc.split("(?=,)");

to split abc

betteroutthanin
  • 7,148
  • 8
  • 29
  • 48
  • This is not the case here, although it works, it complicates the case with regex - specific operator – endriu_l Apr 15 '14 at 10:23
1

This is because split does not include only trailing empty strings, but if you have \n at the end, then the last element is not empty

[a, b, c, d, , , \n]
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

split() is coded to that very job. You can count a character or string occurrence in the below way.

        String ch = ",";
    int count = 0;
    int pos = 0;
    int idx;
    while ((idx = abc.indexOf(ch, pos)) != -1) {
        ++count;
        pos = idx + sub.length();
    }
    System.out.println(count);

This very logic is used in Spring.

Ananth C
  • 20
  • 4