Splitting a String with the pipe symbol as delimiter

前端 未结 6 1986
情话喂你
情话喂你 2021-01-17 10:25

Why is it that in the following, the output is [] and not [1]?

String input=\"1|2|3\";
String[] values= input.split(\"|\");
System.         


        
6条回答
  •  庸人自扰
    2021-01-17 10:49

    you have to escape the character '|' properly

    String input="1|2|3";
            String[] values= input.split("\\|");
            System.out.println("[" + values[0] + "]");
    

提交回复
热议问题