There is a line 1,2,3, "4, A", B, 5, C, 6,7,8. Can you please tell me how to break it up into substrings by a comma but skipping a comma + space? Below is the code that works, maybe there is a simpler option?

String s = "1,2,3,\"4, A\",B, 5,C,6,7,8"; String[] ss; s = s.replaceAll(", ", "#%&"); // меняем "запятая + пробел" на любой символ ss = s.split(","); for (int i = 0; i < ss.length; i++) { System.out.println(ss[i].replaceAll("#%&", ", ")); // возвращаем запятую с пробелом обратно } 

    2 answers 2

    Because split accepts a regular expression, you can use forward lookup with negation :

     String[] ss = s.split(",(?!\\s)"); 

    The expression ,(?!\s) means: “comma, if it is not followed by a space character”. A whitespace character can be any, if you want to ignore the space, then specify it explicitly:

     String[] ss = s.split(",(?! )"); 
       String s = "1,2,3,\"4, A\",B, 5,C,6,7,8"; StringTokenizer st = new StringTokenizer(s, ","); while(st.hasMoreTokens()){ String nextToken = st.nextToken(); if (!nextToken.equals(nextToken.trim())) System.out.print(","); else System.out.println(""); System.out.print(nextToken); } 
      • The second parameter in the StringTokenizer constructor is the set of characters to split. Respectively, this code will break both on a comma, and on a space. - default locale
      • So remove the space from the parameters, but if you don’t need it, add trim to the code ... - Dmitry
      • Firstly, I am not the author of the question :) Secondly, the updated code does not solve the task (omit the comma + space). Compare the output of your code with the sample code in question. - default locale
      • And you run and realize that this code is missing a comma and a space. - Dmitriy
      • Launched, compare the results: ideone.com/qOlrbi - default locale