將Java字符串拆分為管道符號,使用Split(“AC.26”)Java官方文檔指出:弦"boo:and:foo"例如,使用這些表達式Regex結果產生以下結果:{ "boo", "and", "foo" }"這就是我需要它工作的方式。但是,如果我運行這個:public static void main(String[] args){
String test = "A|B|C||D";
String[] result = test.split("|");
for(String s : result){
System.out.println(">"+s+"<");
}
}打?。?gt;<>A<>|<>B<>|<>C<>|<>|<>D<這與我所期望的相去甚遠:>A<>B<>C<><>D<為什么會發生這種事?
3 回答

眼眸繁星
TA貢獻1873條經驗 獲得超9個贊
test.split("\\|");
split
|
OR
\
"\\"
\
\
test.split(Pattern.quote("|"));
Pattern.quote
|
.

慕容森
TA貢獻1853條經驗 獲得超18個贊
public static void main(String[] args) { String test = "A|B|C||D"; String[] result = test.split("\\|"); for (String s : result) { System.out.println(">" + s + "<"); }}
添加回答
舉報
0/150
提交
取消