javaregexexpression

Java Split a String with coma or 0(zero) followed by a whitespace


If I have a string, e.g.

 s = "david,marko,rita,0 megan,0 vivian,law";

and I need to split this string into words and store each word in individualy array position

david
marko
rita
megan
vivian
law

I am trying with

String arr[] = s.split("[,\\s]");

but didn´t work. Any suggestions?


Solution

  • You could use this expression:

    String arr[] = s.split(",0?\\s*");
    

    Or maybe even:

    String arr[] = s.split("[,0\\s]+");
    

    Which one you want is unclear from the example.