Java String split()方法返回意外长度的字符串数组

时间:2017-11-14 02:27:15

标签: java split

String source = "s5 g900 sued_p033178672__.____ ____.__4.5cm"; // __ __ is not a delimiter 

String deli = "__.__"; // this is my separator string to split target

String[] splittedString = source.split(deli, -1);

splittedString.length; // I expected 3 but was 4

我该怎么做才能正确分割目标字符串?

1 个答案:

答案 0 :(得分:1)

String.split()将正则表达式作为输入。因此,.匹配任何字符,因此"__ __"也会被视为分隔符。

要使其仅匹配"__.__",您需要转义特殊字符.,即

String deli = "__\\.__";
相关问题