且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 split("|") 通过管道符号拆分 Java 字符串

更新时间:2023-02-21 13:32:29

您需要

test.split("\|");

split 使用正则表达式,在 regex| 是表示 OR 运算符的元字符.您需要使用 转义该字符(用 String 编写为 "\" 因为 也是字符串文字中的元字符,需要另一个 转义它).

split uses regular expression and in regex | is a metacharacter representing the OR operator. You need to escape that character using (written in String as "\" since is also a metacharacter in String literals and require another to escape it).

你也可以使用

test.split(Pattern.quote("|"));

然后让 Pattern.quote 创建表示 | 的正则表达式的转义版本.

and let Pattern.quote create the escaped version of the regex representing |.