空字符串和单个分隔符字符串上的字符串拆分行为 [英] String split behaviour on empty string and on single delimiter string
本文介绍了空字符串和单个分隔符字符串上的字符串拆分行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是这个问题。
问题出在下面第二行。
"".split("x"); //returns {""} // ok
"x".split("x"); //returns {} but shouldn't it return {""} because it's the string before "x" ?
"xa".split("x"); //returns {"", "a"} // see?, here "" is the first string returned
"ax".split("x"); //returns {"a"}
推荐答案
按照 java.util.regex.Pattern
source ,其中 String.split(..)
使用,
"".split("x"); // returns {""} - valid - when no match is found, return the original string
"x".split("x"); // returns {} - valid - trailing empty strings are removed from the resultant array {"", ""}
"xa".split("x"); // returns {"", "a"} - valid - only trailing empty strings are removed
"ax".split("x"); // returns {"a"} - valid - trailing empty strings are removed from the resultant array {"a", ""}
这篇关于空字符串和单个分隔符字符串上的字符串拆分行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文