[英]Java String split() method returns unexpected length of string array
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
what should I do to split target string properly? 如何正确分割目标字符串?
String.split()
takes a regular expression as its input. String.split()
以正则表达式作为输入。 Therefore, .
因此,
.
matches any characters, so "__ __"
is treated as a delimiter as well. 匹配任何字符,因此
"__ __"
也被视为分隔符。
To make it matches only "__.__"
, you need to escape the special character .
要使其仅匹配
"__.__"
,您需要转义特殊字符.
, ie, ,即
String deli = "__\\.__";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.