Java String split removed empty values

前端 未结 5 1241
无人及你
无人及你 2020-11-21 18:41

I am trying to split the Value using a separator. But I am finding the surprising results

String data = \"5|6|7||8|9||\";
String[] split = data.split(\"\\\\|         


        
5条回答
  •  温柔的废话
    2020-11-21 19:34

    you may have multiple separators, including whitespace characters, commas, semicolons, etc. take those in repeatable group with []+, like:

     String[] tokens = "a , b,  ,c; ;d,      ".split( "[,; \t\n\r]+" );
    

    you'll have 4 tokens -- a, b, c, d

    leading separators in the source string need to be removed before applying this split.

    as answer to question asked:

    String data = "5|6|7||8|9||";
    String[] split = data.split("[\\| \t\n\r]+");
    

    whitespaces added just in case if you'll have those as separators along with |

提交回复
热议问题