Splitting a String with the pipe symbol as delimiter

前端 未结 6 1946
情话喂你
情话喂你 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:52

    Try using \\| instead of | when you split as you need to escape it.

    So your code would change to:

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

提交回复
热议问题