使用管道符号作为分隔符拆分字符串 [英] Splitting a String with the pipe symbol as delimiter

查看:115
本文介绍了使用管道符号作为分隔符拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在下面,输出是 [] 而不是 [1]

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

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

但是,如果我们更改分隔符,则输出为 [1]

However, if we change the separator, the output is [1].

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


推荐答案

试着逃避这个角色:

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

这篇关于使用管道符号作为分隔符拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆