原地址:http://blog.sina.com.cn/s/blog_6f3da9650102x03c.html
public class Split { public static void main(String[] args) { String str1 = "a-b"; String str2 = "a-b-";
String str22 = "a-b--"; String str3 = "-a-b"; String str4 = "-a-b-"; String str5 = "a"; String str6 = "-"; String str7 = "--"; String str8 = ""; //等同于new String() getSplitLen(str1); getSplitLen(str2);
getSplitLen(Str22); getSplitLen(str3); getSplitLen(str4); getSplitLen(str5); getSplitLen(str6); getSplitLen(str7); getSplitLen(str8); } public static void getSplitLen(String demo){ String[] array = demo.split("-"); int len = array.length; System.out.print("\"" + demo + "\"长度为:" + len); if(len >= 0){ for(int i=0; i System.out.print(" \""+array[i]+"\""); } } System.out.println(); } }
"a-b"长度为:2 "a" "b" "a-b-"长度为:2 "a" "b"
"a-b--"长度为:2 "a" "b" "-a-b"长度为:3 "" "a" "b" "-a-b-"长度为:3 "" "a" "b" "a"长度为:1 "a" "-"长度为:0 "--"长度为:0 ""长度为:1 ""
【转】java的string中,关于split空串总会返回单个元素的数组
原文:https://www.cnblogs.com/amibandoufu/p/10821641.html