Java 拆分一个字符串,其中分隔字符串^^^^^对正则表达式有特殊意义
试着像这样分开一根线Java 拆分一个字符串,其中分隔字符串^^^^^对正则表达式有特殊意义,java,regex,Java,Regex,试着像这样分开一根线 12^^^^John Doe^^^^+1897987987 delimiter is the ^^^^ String[] line = str.split("^^^^"); id = line[0]; // code below does not work because there is only one element in array name = line[1]; number = line[2]; split方法不进行拆分,而是
12^^^^John Doe^^^^+1897987987
delimiter is the ^^^^
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
split方法不进行拆分,而是返回整行和单长度数组
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
^在正则表达式中有特殊的含义,因此我认为必须以不同的方式传递此拆分参数才能达到预期的结果,请告知。您需要转义这些字符-
^
表示正则表达式中的“行开始”
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
String[]line=str.split(\\^\\\^\\\^^)代码>
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];
需要两个\
,否则Java会尝试将\^
解释为字符串转义字符,如\n
或\t
String[] line = str.split("^^^^");
id = line[0];
// code below does not work because there is only one element in array
name = line[1];
number = line[2];