String oneTextValue = "1223)234";
String[] chunks = oneTextValue.split("\\)");
String oneTextValue2 = "1223)234)";
String[] chunks2 = oneTextValue2.split("\\)");
String oneTextValue3 = "1223234";
String[] chunks3 = oneTextValue3.split("\\)");
String oneTextValue4 = ")1223234";
String[] chunks4 = oneTextValue4.split("\\)");
![在这里插入图片描述](https://img-blog.csdnimg.cn/d4604edd6c0140cfbaa3cd575c3189bd.png) **现象**
String[] chunks = oneTextValue.split("\\)");
String oneTextValue2 = "1223)234)";
String[] chunks2 = oneTextValue2.split("\\)");
String oneTextValue3 = "1223234";
String[] chunks3 = oneTextValue3.split("\\)");
String oneTextValue4 = ")1223234";
String[] chunks4 = oneTextValue4.split("\\)");
结尾空字符串被丢弃的问题
第2个例子中结果并不是 {“1223”, “234”, “”}, 而是 {“1223”, “234”}
避免结尾空字符串丢弃
结果:
转义字符
1. 如果用“.”作为分隔的话,必须是如下写法:String.split(“\.”),这样才能正确的分隔开,不能用String.split(“.”);
2. 如果用“|”作为分隔的话,必须是如下写法:String.split(“\|”),这样才能正确的分隔开,不能用String.split(“|”); “.”和“|”都是转义字符,必须得加"\";
多个分隔符
- 如果在一个字符串中有多个分隔符,可以用“|”作为连字符
结果
{“12”, “23”, “234”}