Toxing a string containing empty tokens

I have a seemingly simple problem of separating commas separated Stringby tokens, as a result of which the output should contain empty tokens in cases where:

  • The first character in Stringis a comma.
  • The last character in Stringis a comma.
  • Two consecutive commas occur.

For example, to String: ",abd,def,,ghi,"the result to appear: {"", "abd", "def", "", "ghi", ""}.

I tried to use String.split, Scannerand StringTokenizerfor this, but each of them gives a different undesirable conclusion (examples below). Can anyone suggest an elegant solution for this, preferably using JDK classes? Obviously, I could encode something, but I feel that I am missing something on one of the three approaches mentioned. Note that the delimiter is fixed, Stringalthough not necessarily a comma, not a single character.

Code example

import java.util.*;

public class Main12 {
  public static void main(String[] args) {
    String s = ",abd,def,,ghi,";
    String[] tokens = s.split(",");

    System.err.println("--- String.split Output ---");
    System.err.println(String.format("%s -> %s", s, Arrays.asList(tokens)));

    for (int i=0; i<tokens.length; ++i) {
      System.err.println(String.format("tokens[%d] = %s", i, tokens[i]));
    }

    System.err.println("--- Scanner Output ---");

    Scanner sc = new Scanner(s);
    sc.useDelimiter(",");
    while (sc.hasNext()) {
      System.err.println(sc.next());
    }

    System.err.println("--- StringTokenizer Output ---");

    StringTokenizer tok = new StringTokenizer(s, ",");
    while (tok.hasMoreTokens()) {
      System.err.println(tok.nextToken());
    }
  }
}

Output

$ java Main12
--- String.split Output ---
,abd,def,,ghi, -> [, abd, def, , ghi]
tokens[0] =
tokens[1] = abd
tokens[2] = def
tokens[3] =
tokens[4] = ghi
--- Scanner Output ---
abd
def

ghi
--- StringTokenizer Output ---
abd
def
ghi
+5
source share
1 answer

Pass -1to splitas an argument limit:

String s = ",abd,def,,ghi,";
String[] tokens = s.split(",", -1);

Then your result array will contain any trailing blank lines.

javadocs:

[] , , . [] , , , , .

split(regex) , limit 0, .

+12

All Articles