{"id":682,"date":"2023-11-28T09:01:01","date_gmt":"2023-11-28T01:01:01","guid":{"rendered":""},"modified":"2023-11-28T09:01:01","modified_gmt":"2023-11-28T01:01:01","slug":"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f","status":"publish","type":"post","link":"https:\/\/mushiming.com\/682.html","title":{"rendered":"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f"},"content":{"rendered":"

\u80cc\u666f<\/h1>\n

\u5b57\u7b26\u4e32\u62fc\u63a5\u4e0d\u7ba1\u662f\u5728\u4e1a\u52a1\u4e0a\uff0c\u8fd8\u662f\u5199\u7b97\u6cd5\u65f6\u90fd\u4f1a\u9891\u7e41\u4f7f\u7528\u5230\u3002\u5bf9\u4e8eJava\u6765\u8bf4\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u6709\u7740\u5f88\u591a\u79cd\u65b9\u5f0f\uff0c\u4ed6\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff0c\u5bf9\u5e94\u4e0d\u540c\u7684\u4e1a\u52a1\u54ea\u79cd\u66f4\u597d\u7528\u5462\u3002<\/p>\n

\"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f<\/p>\n

String\u5e95\u5c42\u539f\u7406<\/h1>\n

\u5728\u8ba8\u8bba\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\uff0c\u9996\u5148\u9700\u8981\u77e5\u9053String\u7684\u5e95\u5c42\u539f\u7406\u3002<\/p>\n

\u6211\u4eec\u8fd9\u91cc\u53ea\u8ba8\u8bbajdk1.8\u4e4b\u540e\u7684\u60c5\u51b5\uff0c\u770b\u4e0b\u7ed3\u6784<\/p>\n

private<\/span> final<\/span> byte<\/span>[] value;\n<\/code><\/pre>\n

\u8fd9\u4e00\u884c\u4ee3\u7801\u5df2\u7ecf\u53ef\u4ee5\u8bf4\u660e\u5f88\u591a\u4e1c\u897f\u3002\u5b57\u7b26\u4e32\u5b9e\u8d28\u5c31\u662f\u4e0d\u53ef\u53d8\u7684byte\u6570\u7ec4\u3002\u56e0\u4e3a\u4e0d\u53ef\u53d8\uff0c\u6240\u4ee5\u5bf9\u4ed6\u8fdb\u884c\u62fc\u63a5\u5bf9\u4ed6\u62fc\u63a5\u5b9e\u9645\u5c31\u662f\u751f\u6210\u4e86\u591a\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u5c31\u662f\u4e0d\u9f13\u52b1\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884c\u62fc\u63a5\u7684\u539f\u56e0\u3002\u4f46\u4e0d\u53ef\u53d8\u4e5f\u6709\u5f88\u591a\u597d\u5904\uff0c\u4f8b\u5982\u7ebf\u7a0b\u5b89\u5168\u3001\u53ef\u4ee5\u5b58\u5728\u5b57\u7b26\u4e32\u7f13\u51b2\u6c60\u590d\u7528\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n

\u62fc\u63a5\u7684\u65b9\u6cd5<\/h1>\n

\u7ecf\u5178\u4f46\u6709\u65f6\u4e0d\u4f18\u96c5\u7684 +<\/h2>\n
String a<\/span> = \"123\"<\/span>;<\/span>\nString b<\/span> = \"456\"<\/span>;<\/span>\nString c<\/span> = a + b;<\/span>\n<\/code><\/pre>\n

c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5c31\u662fab\u62fc\u63a5\u8d77\u6765\u7684\u5b57\u7b26\u4e32\uff0c\u201c123456\u201d<\/p>\n

\u8fd9\u6bb5\u4ee3\u7801\u53cd\u7f16\u8bd1\u51fa\u6765\u7684\u4ee3\u7801\u662f<\/p>\n

String c = (new StringBuilder()).append<\/span>(a).append<\/span>(b).toString<\/span>();\n\u200b\n<\/code><\/pre>\n

\u53ef\u4ee5\u770b\u51fa\u8fd9\u4e2a +<\/strong> \u662fJava\u7684\u8bed\u6cd5\u7cd6\uff0c\u4ed6\u5b9e\u9645\u4e0a\u662f\u8c03\u7528\u7684StringBuilder<\/strong>\uff0c\u901a\u8fc7append()\u6765\u8fdb\u884c\u62fc\u63a5\u3002\u5173\u4e8eStringBuilder\u6211\u4eec\u540e\u9762\u518d\u8bb2\uff0c\u5148\u6765\u8bb2\u4e0b\u8fd9\u4e2a\u7528\u6cd5\u7684\u4f18\u7f3a\u70b9\u3002<\/p>\n

\u4f18\u70b9<\/h3>\n

\u201c+\u201d\uff0c\u6700\u5927\u7684\u4f18\u70b9\u5c31\u662f\u7b80\u6d01<\/strong>\u3002\u5982\u679c\u4e24\u4e2a\u5b57\u7b26\u4e32\u9700\u8981\u9996\u5c3e\u62fc\u63a5\uff0c+\u53f7\u4e49\u4e0d\u5bb9\u8f9e\u7684\u6210\u4e3a\u4e86\u6700\u597d\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n

\u7f3a\u70b9<\/h3>\n

\u8bf4\u5230\u7f3a\u70b9\u7684\u8bdd\u5c31\u591a\u4e86\u3002\u7b80\u6d01\u4e5f\u662f\u4ed6\u7684\u6700\u5927\u7f3a\u70b9\uff0c\u4e5f\u5c31\u662f\u4e0d\u591f\u7075\u6d3b<\/strong>\u3002<\/p>\n

\u4e1a\u52a1\u4e00<\/h4>\n

\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32List\uff0c\u6211\u9700\u8981\u628a\u4ed6\u4eec\u62fc\u63a5\u8d77\u6765\uff0c\u600e\u4e48\u529e\uff1f<\/p>\n

for<\/span>(String tmp:list){\n    s += tmp;\n}\n<\/code><\/pre>\n

\u7b80\u6d01\u7684\u4e00\u6279\uff0c\u4f46\u662f\u4ed6\u9690\u85cf\u7740\u5f88\u5927\u7684\u95ee\u9898\uff01<\/p>\n

\"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f<\/p>\n

\u4e0a\u9762\u8bf4\u5230\u8fd9\u79cd\u62fc\u63a5\u65b9\u5f0f\u5b9e\u9645\u662f\u901a\u8fc7StringBuilder\u7684append\u7684\u65b9\u6cd5\u3002\u4f60\u4e0d\u9700\u8981\u77e5\u9053\u4ed6\u7684\u539f\u7406\uff0c\u4f60\u53ea\u9700\u8981\u77e5\u9053\uff0c\u6bcf\u6b21\u5faa\u73af\uff0c\u4ed6\u90fd\u4f1anew\u4e00\u4e2aStringBuilder\u5bf9\u8c61\u3002\u521b\u5efa\u5bf9\u8c61\u7684\u5f00\u9500\u662f\u5f88\u5927\u7684\uff0c\u5982\u679cList\u6709\u51e0\u5343\u51e0\u4e07\uff0c\u5185\u5b58\u5f00\u9500\u548c\u65f6\u95f4\u5f00\u9500\u662f\u4e0d\u80fd\u63a5\u53d7\u7684\uff01<\/p>\n

\u6240\u4ee5\u963f\u91cc\u5df4\u5df4\u7684\u89c4\u8303\u8bf4\u5230\uff1a<\/p>\n

\"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f<\/p>\n

\u8868\u9762\u4e0a\u662f\u63a8\u8350\uff0c\u5b9e\u9645\u5c31\u662f\u7981\u6b62<\/strong>\u3002\u5199\u7b97\u6cd5\u4f1a\u6d88\u8017\u5927\u91cf\u65f6\u95f4\u5bfc\u81f4\u4e0d\u901a\u8fc7\uff0c\u4e1a\u52a1\u4e5f\u4f1a\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u63d0\u9ad8\u4e86\u65e0\u6545\u7684\u5f00\u9500\uff0c\u5c5e\u4e8e\u9886\u5bfc\u770b\u4e86\u60f3\u6253\u6b7b\u7684\u4ee3\u7801\u3002<\/p>\n

\u4e1a\u52a1\u4e8c<\/h4>\n

\u5927\u5bb6\u597d\uff0c\u6211\u53ebXX\uff0c\u6211\u662f\u6765\u81eaXXX\u5b66\u6821\u7684\u5927X\u5b66\u751f\uff0c\u6211\u7684\u7231\u597d\u662fXXX\u3002<\/p>\n

\u4e00\u4e2a\u7ecf\u5178\u7684\u6a21\u677f\uff0c\u6211\u9700\u8981\u66ff\u6362\u6389\u4e2d\u95f4\u7684XXX\u4e3acontroller\u7684\u53c2\u6570\uff0c\u600e\u4e48\u529e\u5462\uff1f<\/p>\n

String s<\/span> = \"\u5927\u5bb6\u597d\uff0c\u6211\u53eb\"<\/span>+name+\"\u6211\u662f\u6765\u81ea\"<\/span>+school+\"\u5b66\u6821\u7684\u5927\"<\/span>+num+\u201c\u5b66\u751f\uff0c\u6211\u7684\u7231\u597d\u662f\u201d+aihao;<\/span>\n<\/code><\/pre>\n

\u5c5e\u4e8e\u53ef\u7528\u4f46\u6781\u5176\u4e11\u964b<\/strong>\u7684\u4ee3\u7801\u3002\u5982\u679c\u5176\u4ed6\u63a5\u53e3\u4e5f\u9700\u8981\u8fd9\u4e2a\u6a21\u677f\uff0c\u6211\u8fd8\u8981\u628a\u8fd9\u6bb5\u8bdd\u590d\u5236\u5230\u6240\u6709\u4f4d\u7f6e\u4e0a\u5417\uff1f\u5982\u679c\u6211\u8981\u6539\u52a8\u8fd9\u4e2a\uff0c\u6211\u8981\u5bf9\u6240\u6709\u4ee3\u7801\u8fdb\u884c\u6539\u52a8\u5417\u3002<\/p>\n

\u4e07\u80fd\u7684StringBuilder<\/h2>\n

\u5148\u4ecb\u7ecd\u4e0bStringBuilder\u7684\u539f\u7406\u3002\u628a\u5b57\u7b26\u4e32\u62fc\u63a5\u60f3\u8c61\u6210\u6570\u7ec4\u5c31\u5f88\u597d\u7406\u89e3\u4e86\uff0cStringBuilder\u6709\u70b9\u7c7b\u4f3c\u4e8eArrayList\uff0c\u53ef\u53d8\u6570\u7ec4\u3002<\/p>\n

    \/**     * The value is used for character storage.     *\/<\/span>\n    char[]<\/span> value;\n<\/code><\/pre>\n

\u533a\u522b\u5c31\u662f\u6ca1\u6709final\u4fee\u9970<\/strong>\uff0c\u5f53\u5230\u8fbe\u9608\u503c\u65f6\u8fdb\u884c\u6269\u5bb9\u64cd\u4f5c\u3002append\u65b9\u6cd5\u5c31\u662f\u5f80\u540e\u63d2\u5165\u3002<\/p>\n

\u90a3\u4e48\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e0a\u9762\u4e1a\u52a1\u4e00\u7684\u95ee\u9898\u4e86\u3002<\/p>\n

StringBuilder sb<\/span> = new StringBuilder();<\/span>\nfor(String tmp:list){\n    sb.append(tmp);<\/span>\n}\nString s<\/span> = sb.tostring();<\/span>\n<\/code><\/pre>\n

\u76f8\u6bd4\u4e8e\u4e0a\u9762\uff0c\u53ea\u521b\u5efa\u4e86\u4e00\u4e2aStringBuilder\u5bf9\u8c61\uff0c\u51cf\u5c11\u5faa\u73af\u521b\u5efa\u7684\u5f00\u9500\u3002<\/p>\n

\u7ebf\u7a0b\u5b89\u5168\u7684StringBuffer<\/h2>\n

StringBuffer\u4e0eStringBuilder\u76f8\u6bd4\uff0c\u6709\u7ebf\u7a0b\u5b89\u5168\u7684\u4f18\u52bf\uff0c\u901a\u8fc7\u4e0a\u9501\u7684\u65b9\u5f0f\u3002\u540c\u65f6\u5bfc\u81f4\u6548\u7387\u7565\u4f4e\u4e8eStringBuilder\u3002<\/p>\n

\u7075\u6d3b\u7684String.format()<\/h2>\n

\u8fd9\u4e2a\u4e25\u683c\u6765\u8bf4\u5e94\u8be5\u53eb\u505a\u683c\u5f0f\u5316\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u6765\u62fc\u63a5\u3002<\/p>\n

\u719f\u6089c\u8bed\u8a00\u7684\u5e94\u8be5\u80fd\u591f\u61c2\uff0c\u6211\u8fd9\u91cc\u4e3e\u4e00\u4e2a\u4f8b\u5b50<\/p>\n

String<\/span> msg = String<\/span>.format(\u201c\u6211\u662f%s\u5c0f\u5b66\u7684\u5b66\u751f\uff0c\u6211\u7231\u5403%s\u201d,\"\u9633\u5149\"<\/span>,\"\u5c4e\"<\/span>);\n\/\/\u8f93\u51fa \u6211\u662f\u9633\u5149\u5c0f\u5b66\u7684\u5b66\u751f\uff0c\u6211\u7231\u5403\u5c4e<\/span>\n<\/code><\/pre>\n

\u4f7f\u7528\u5b57\u7b26\u4e32\u94fe\u4ee3\u66ff%s\uff0c\u751f\u6210\u9700\u8981\u7684\u5b57\u7b26\u4e32\u3002\u4e5f\u4e0d\u4ec5\u53ef\u4ee5\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u770b\u4e0b\u4e0b\u56fe\uff08\u5077\u7684\u56fe\uff0c\u6ca1\u5168\u90e8\u9a8c\u8bc1\u8fc7\uff0c\u9519\u4e86\u522b\u627e\u6211\uff09<\/p>\n

\"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f<\/p>\n

\u8fd9\u79cd\u65b9\u5f0f\u5c31\u89e3\u51b3\u4e86\u4e1a\u52a1\u4e8c\u7684\u95ee\u9898\u3002\u901a\u8fc7\u7f16\u5199\u679a\u4e3e\u6216\u8005\u5e38\u91cf\u5b57\u7b26\u4e32\u7559\u51fa\u5bf9\u5e94\u7684\u4f4d\u7f6e\uff0c\u4f7f\u7528\u65f6\u518d\u7528String.format()\u62fc\u63a5<\/strong>\u3002<\/p>\n

\u6709\u70b9\u7eff\u8272\u7684concat<\/h2>\n

\u4e3a\u4ec0\u4e48\u8bf4\u4ed6\u7eff\u8272\u5462\uff0c\u5c31\u662f\u6211\u8fd8\u6ca1\u6709\u627e\u5230\u4ed6\u6709\u4ec0\u4e48\u4f18\u52bf\u3002<\/p>\n

String s<\/span> = \"123\"<\/span>.concat(\"456\"<\/span>);<\/span>\n\/\/\u7ed3\u679c\u7b49\u4ef7\u4e8e\nString s<\/span> = \"123\"<\/span> + \"456\"<\/span>;<\/span>\n<\/code><\/pre>\n

concat\u65b9\u6cd5\u7684\u539f\u7406\u662f\u6570\u7ec4\u6269\u5bb9\u540e\u590d\u5236\u4e4b\u524d\u7684\u5185\u5bb9\u5e76\u5199\u65b0\u7684\u5185\u5bb9\uff0c\u548cStringBuilder\u5e95\u5c42\u6709\u70b9\u76f8\u50cf\u3002<\/p>\n

\u4f46\u662f\u76f8\u6bd4\u4e8e\u201c+\u201d\u53f7\u6765\u8bf4\uff0c\u65e2\u4e0d\u7b80\u4fbf\uff0c\u53c8\u6ca1\u6709\u4ec0\u4e48\u6548\u7387\u4e0a\u7684\u63d0\u9ad8\u3002\u5728\u5faa\u73af\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u6761\u4ef6\uff0c\u6548\u7387\u4e0a\u4f1a\u7565\u6709\u4e00\u70b9\u4f18\u52bf\uff0c\u4f46\u662f\u8fd9\u79cd\u60c5\u51b5\u662f\u6839\u672c\u4e0d\u88ab\u5141\u8bb8\u7684\uff0c\u6240\u4ee5concat\u5c31\u5f88\u9e21\u808b\u3002<\/p>\n

JDK1.8\u4f18\u96c5\u5199\u6cd5<\/h2>\n

\u521a\u624d\u63d0\u5230\u4e1a\u52a1\u4e00\u7684\u89e3\u51b3\u529e\u6cd5\u53ef\u4ee5\u4f7f\u7528\u6734\u7d20\u7684StringBuilder\u6765\u89e3\u51b3\uff0c\u4f46\u662f\u5bf9\u4e8e\u4e1a\u52a1\u4ee3\u7801\u6765\u8bf4\u6709\u4e00\u70b9\u5197\u957f\u3002<\/p>\n

Jdk1.8\u7ed9\u51fa\u4e86\u4f18\u96c5\u7684\u7b54\u6848<\/p>\n

String s<\/span> = String.join(\"_\"<\/span>, list);<\/span>\n<\/code><\/pre>\n

\u4e00\u884c\u4ee3\u7801\uff0c\u5c31\u53ef\u4ee5\u628alist\u91cc\u7684\u5b57\u7b26\u4e32\u901a\u8fc7\u201c_\u201d\u62fc\u63a5\u8d77\u6765\u3002<\/p>\n

\u7ecf\u5178\u7684Guava<\/h2>\n

guava\u662f\u6211\u4eeccrud\u7a0b\u5e8f\u5458\u7684\u597d\u4f19\u4f34\uff0c\u8fd9\u91cc\u5c31\u4e0d\u7528\u591a\u8bf4\u4e86\u3002\u6211\u4eec\u6700\u5e38\u63a5\u89e6\u5230\u7684\u5176\u5b9e\u5c31\u662fguava\u7684\u672c\u5730\u7f13\u5b58\u548c\u5b57\u7b26\u4e32\u64cd\u4f5c\u3002<\/p>\n

String result<\/span> = Joiner.on<\/span>(\",\"<\/span>).join(list);<\/span>\n<\/code><\/pre>\n

\u4e5f\u662f\u7b80\u6d01\u7684\u4e00\u53e5\u8bdd\uff0c\u4f46\u662f\u76f8\u6bd4\u4e8ejdk\u672c\u571f\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u6765\u8bf4\uff0c\u4ed6\u8fd8\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u7279\u6027\u3002\u4f8b\u5982\u53ef\u4ee5\u628a\u4e3anull\u7684\u6570\u7ec4\u7ed9\u8df3\u8fc7\u6216\u8005\u66ff\u6362\u6389\u7b49\u7b49\u3002\u529f\u80fd\u8981\u6bd4jdk\u7684\u8981\u4e30\u5bcc\u4e00\u70b9\u3002\u5728\u6b63\u5e38\u7684web\u9879\u76ee\u91cc\u57fa\u672c\u90fd\u4f1a\u6709Guava\u7684\u4f9d\u8d56\uff0c\u4f7f\u7528\u8d77\u6765\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\u3002<\/p>\n

\u603b\u7ed3<\/h1>\n

\u8fd9\u7bc7\u6587\u7ae0\u504f\u91cd\u4e8e\u4ee3\u7801\u7f16\u5199\u65b9\u9762\uff0c\u5982\u4f55\u5199\u51fa\u7b80\u6d01\u9ad8\u6548\u7684\u4ee3\u7801\uff0c\u662f\u6211\u4eec\u8981\u8ffd\u6c42\u7684\u3002\u4e0d\u8981\u8ba9\u4f60\u5199\u7684\u5783\u573e\u4ee3\u7801\u6076\u5fc3\u5230\u63a5\u624b\u7684\u540c\u4e8b\u5c31\u597d\u4e86\u3002<\/p>\n

\"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f<\/p>\n","protected":false},"excerpt":{"rendered":"Java\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u4f18\u96c5\u65b9\u5f0f\u5b57\u7b26\u4e32\u62fc\u63a5\u4e0d\u7ba1\u662f\u5728\u4e1a\u52a1\u4e0a\uff0c\u8fd8\u662f\u5199\u7b97\u6cd5\u65f6\u90fd\u4f1a\u9891\u7e41\u4f7f\u7528\u5230\u3002\u5bf9\u4e8eJava\u6765\u8bf4\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u6709\u7740\u5f88\u591a\u79cd\u65b9\u5f0f...","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\/682"}],"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=682"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/682\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}