{"id":578,"date":"2023-09-16T16:54:01","date_gmt":"2023-09-16T08:54:01","guid":{"rendered":""},"modified":"2023-09-17T00:41:54","modified_gmt":"2023-09-16T16:41:54","slug":"%e3%80%90java%e3%80%91%e6%95%b0%e7%bb%84%e8%bd%ac%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/mushiming.com\/578.html","title":{"rendered":"\u3010Java\u3011\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n \u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4<\/p>\n \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/p>\n \u6a21\u62df\u5b9e\u73b0\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/p>\n \u4f7f\u7528Java split() \u65b9\u6cd5<\/p>\n split() \u65b9\u6cd5\u6839\u636e\u5339\u914d\u7ed9\u5b9a\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u62c6\u5206\u5b57\u7b26\u4e32\u3002<\/p>\n \u6ce8\u610f\uff1a<\/strong> . \u3001 | \u548c * \u7b49\u8f6c\u4e49\u5b57\u7b26\uff0c\u5fc5\u987b\u5f97\u52a0 \\\u3002\u591a\u4e2a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u7528 | \u4f5c\u4e3a\u8fde\u5b57\u7b26\u3002<\/p>\n \u65b9\u6cd5:1\uff1a\u904d\u5386<\/em><\/p>\n \u65b9\u6cd52\uff1a\u4f7f\u7528StringUtils\u7684join\u65b9\u6cd5<\/em><\/p>\n \u65b9\u6cd53\uff1a\u4f7f\u7528ArrayUtils\u7684toString\u65b9\u6cd5<\/em><\/p>\n \u8fd9\u6bb5\u4ee3\u7801\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u6765\u5b8c\u6210\u529f\u80fd\u7684\u3002\u5148\u4ece\u5de6\u8fb9\u7684 [ <\/strong>\u5f00\u59cb\uff0c\u518d\u62fc\u5143\u7d20<\/strong>\u548c\u9017\u53f7<\/strong>\uff0c\u6700\u540e\u5728\u62fc ]<\/strong><\/em><\/p>\n Java\u7684\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\u662f\u76f4\u63a5\u5224\u65ad\u7b49\u4e0d\u7b49\u4e8enull\uff0c\u800c\u4e0d\u662f\u4f7f\u7528assert\u65ad\u8a00\u3002<\/em><\/p>\n <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"\u3010Java\u3011\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32\u8f93\u51fa\uff0c\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4_javastring\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4<\/h1>\n
\/\/ \u5b57\u7b26\u4e32\u8f6c\u6570\u7ec4 java.lang.String\n \nString str = \"0,1,2,3,4,5\";\n \nString[] arr = str.split(\",\"); \/\/ \u7528,\u5206\u5272\n \nSystem.out.println(Arrays.toString(arr)); \/\/ [0, 1, 2, 3, 4, 5]<\/code><\/pre>\n<\/p>\n<\/p>\n
\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/h1>\n
String[] arr = { \"0\", \"1\", \"2\", \"3\", \"4\", \"5\" };\n \n\/\/ \u904d\u5386\n \nStringBuffer str5 = new StringBuffer();\n \nfor (String s : arr) {\n \n str5.append(s);\n \n}\n \nSystem.out.println(str5.toString()); \/\/ 012345<\/code><\/pre>\n<\/p>\n<\/p>\n
\/\/\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32 org.apache.commons.lang3.StringUtils\n \nString str3 = StringUtils.join(arr); \/\/ \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32,\u5176\u5b9e\u4f7f\u7528\u7684\u4e5f\u662f\u904d\u5386\n \nSystem.out.println(str3); \/\/ 012345\n \nString str4 = StringUtils.join(arr, \",\"); \/\/ \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32(\u9017\u53f7\u5206\u9694)(\u63a8\u8350)\n \nSystem.out.println(str4); \/\/ 0,1,2,3,4,5<\/code><\/pre>\n<\/p>\n<\/p>\n
\/\/ \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32 org.apache.commons.lang3.ArrayUtils\n \nString str2 = ArrayUtils.toString(arr, \",\"); \/\/ \u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32(\u9017\u53f7\u5206\u9694,\u9996\u5c3e\u52a0\u5927\u62ec\u53f7)\n \nSystem.out.println(str2); \/\/ {0,1,2,3,4,5}<\/code><\/pre>\n<\/p>\n<\/p>\n
\u6a21\u62df\u5b9e\u73b0\u6570\u7ec4\u8f6c\u5b57\u7b26\u4e32<\/h1>\n
public static String MyTostring(int[] array) {\n \/\/\u5224\u65ad\u6570\u7ec4\u662f\u5426\u662fnull\n if (null == array) {\n return null;\n }\n String ch = \"[\";\/\/\u5b58\u50a8\u5de6\u8fb9\u7684\u62ec\u53f7\n for (int i = 0; i < array.length; i++) {\n ch += array[i];\/\/\u5c06\u4e0b\u6807\u4e3ai\u7684\u5143\u7d20\u62fc\u63a5\u5230ch\u91cc\n \/\/\u5224\u65ad\u662f\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20 - \u6700\u540e\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u6ca1\u6709\u9017\u53f7\u3002\n if (i != array.length - 1 ) {\/\/\u4e0d\u662f\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u5c31\u52a0\u4e0a\u4e00\u4e2a\u9017\u53f7\n ch += \",\";\n }\n }\n \/\/\u6570\u7ec4\u904d\u5386\u5b8c\u6bd5 - \u9017\u53f7\u4e5f\u6dfb\u52a0\u5b8c\u6bd5 - \u53ea\u5dee\u53f3\u8fb9\u7684\u62ec\u53f7\u4e86\n ch += \"]\";\n return ch;\n}\n\npublic static void main(String[] args) {\n int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};\n String ret = MyTostring(array);\n System.out.println(ret);\n}<\/code><\/pre>\n