且构网

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

如何拆分字符串 |(管道)在 Java 中

更新时间:2023-02-21 12:38:39

您必须转义管道字符 (split 将正则表达式作为参数,因此 "|" 是 控制字符):

You'll have to escape your pipe character (split takes a regular expression as argument and therefore "|" is a control character):

str.split("\|");

请注意:结果数组的开头包含一个空字符串,因为您有|"在字符串的开头.

Please note: the resulting array contains an empty string at the beginning since you have "|" at start of your string.