java split小结
程序员文章站
2024-03-23 14:46:04
...
java的split的方法的顺序是:匹配,切割,去除尾部(为空)
1.
String[] strs = "".split(",") //返回结果:[""]
2.
String[] strs = ",,a,b"; //返回结果:["", "", "a", "b"]
3.
String[] strs = "a,b,,"; //返回结果:["a", "b"]
如果想简单的做到,",,a,b,c,,"返回["a", "b", "c"] 还是使用apache的commons包中的StringUtils.split
String[] strs = StringUtils.split(",,a,b,,c,,", ",");//返回["a", "b", "c"]
更多使用方式查看StringUtils的api。
在测试不同语言的split的方法时,python的处理方式和java类似,但javascript的处理方式不同。
javascript不会取出尾部的空。
node>console.log("a,b,c,".split(","));; ["a", "b", "c", ""]
最近在学习clojure,也研究了下clojure对于字符串的处理
(seq (.split #"," ",a,b,c")) ;; ["", "a", "b", "c"] look like java (re-seq #"\w+" "one-two/three") ;; ["one" "two" "three"]