{"id":65439,"date":"2025-05-03T09:48:16","date_gmt":"2025-05-03T01:48:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/65439\/"},"modified":"2025-05-03T09:48:16","modified_gmt":"2025-05-03T01:48:16","slug":"java%e6%89%93%e5%8d%b0%e7%b1%bb%e6%95%b0%e7%bb%84%e6%80%8e%e4%b9%88%e5%af%b9%e9%bd%90-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/65439\/","title":{"rendered":"java\u6253\u5370\u7c7b\u6570\u7ec4\u600e\u4e48\u5bf9\u9f50"},"content":{"rendered":"<blockquote><p>\n  java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4\u65f6\uff0c\u53ef\u4f7f\u7528 string.format() \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u5143\u7d20\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5bf9\u9f50\u65b9\u5f0f\uff1a\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a\u6307\u5b9a\u5360\u4f4d\u7b26 (&#8220;%s&#8221; \u7528\u4e8e\u5b57\u7b26\u4e32) \u548c\u5bf9\u9f50\u65b9\u5f0f\uff08\u4f7f\u7528\u5b57\u6bcd\uff1a- \u5de6\u5bf9\u9f50\uff0c+ \u52a0\u6b63\u8d1f\u53f7\uff0c \uff1a\u4e0d\u52a0\u7b26\u53f7\uff0c0 \u53f3\u5bf9\u9f50\uff09\u3002\u6307\u5b9a\u5b57\u6bb5\u5bbd\u5ea6\uff1a\u6570\u5b57\u8868\u793a\u5143\u7d20\u5360\u7528\u7684\u5b57\u7b26\u6570\u3002\u4f7f\u7528 string.format() \u65b9\u6cd5\uff1a\u4f20\u5165\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u8981\u6253\u5370\u7684\u5143\u7d20\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/13\/2024111301012035452.jpg\" class=\"aligncenter\" title=\"java\u6253\u5370\u7c7b\u6570\u7ec4\u600e\u4e48\u5bf9\u9f50\u63d2\u56fe\" alt=\"java\u6253\u5370\u7c7b\u6570\u7ec4\u600e\u4e48\u5bf9\u9f50\u63d2\u56fe\" \/><\/p>\n<p><strong>Java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4<\/strong><\/p>\n<p>\u5728 Java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 String.format() \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u5143\u7d20\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u521b\u5efa\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u8868\u793a\u5143\u7d20\u7684\u5360\u4f4d\u7b26\uff08\u4f8b\u5982\uff0c&#8221;%s&#8221; \u7528\u4e8e\u5b57\u7b26\u4e32\uff09\u3002<\/li>\n<li>\u4f7f\u7528 % \u7b26\u53f7\u6307\u5b9a\u5360\u4f4d\u7b26\uff0c\u540e\u8ddf\u4e00\u4e2a\u5b57\u6bcd\u8868\u793a\u5bf9\u9f50\u65b9\u5f0f\u548c\u5b57\u6bb5\u5bbd\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\uff1a<\/strong><\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u5b57\u6bcd\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>-\uff1a\u5de6\u5bf9\u9f50<\/li>\n<li>+\uff1a\u5728\u6570\u5b57\u524d\u52a0\u6b63\u8d1f\u53f7<\/li>\n<li> \uff1a\u6570\u5b57\u4e0d\u52a0\u7b26\u53f7\uff08\u65e0\u7b26\u53f7\u586b\u5145\uff09<\/li>\n<li>0\uff1a\u53f3\u5bf9\u9f50\uff08\u7528\u96f6\u586b\u5145\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6307\u5b9a\u5b57\u6bb5\u5bbd\u5ea6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5bf9\u9f50\u5b57\u6bcd\u540e\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u5b57\u6bb5\u5bbd\u5ea6\u3002\u8fd9\u5c06\u63a7\u5236\u5143\u7d20\u5728\u8f93\u51fa\u4e2d\u5360\u7528\u7684\u5b57\u7b26\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u4f7f\u7528 String.format() \u65b9\u6cd5\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 String.format() \u65b9\u6cd5\uff0c\u4f20\u5165\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u8981\u6253\u5370\u7684\u5143\u7d20\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5bf9\u9f50\u6253\u5370\u4e00\u4e2a String[] \u6570\u7ec4\uff1a<\/p>\n<pre>String[] names = {\"John\", \"Mary\", \"Bob\"};\n\n\/\/ \u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\nString format = \"%-15s\";\n\n\/\/ \u904d\u5386\u6570\u7ec4\u5e76\u6253\u5370\u5bf9\u9f50\u540e\u7684\u5143\u7d20\nfor (String name : names) {\n    System.out.printf(format, name);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>John           \nMary           \nBob            <\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u683c\u5f0f\u5b57\u7b26\u4e32 &#8220;%-15s&#8221; \u6307\u5b9a\u5143\u7d20\u5de6\u5bf9\u9f50\u5e76\u5360\u636e 15 \u4e2a\u5b57\u7b26\u7684\u5bbd\u5ea6\u3002\u7ed3\u679c\u662f\u5143\u7d20\u4ee5\u56fa\u5b9a\u5bbd\u5ea6\u5bf9\u9f50\u6253\u5370\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjava\u6253\u5370\u7c7b\u6570\u7ec4\u600e\u4e48\u5bf9\u9f50\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8IDCBABY\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4\u65f6\uff0c\u53ef\u4f7f\u7528 string.format() \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u5143\u7d20\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5bf9\u9f50\u65b9\u5f0f\uff1a\u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a\u6307\u5b9a\u5360\u4f4d\u7b26 (&#8220;%s&#8221; \u7528\u4e8e\u5b57\u7b26\u4e32) \u548c\u5bf9\u9f50\u65b9\u5f0f\uff08\u4f7f\u7528\u5b57\u6bcd\uff1a- \u5de6\u5bf9\u9f50\uff0c+ \u52a0\u6b63\u8d1f\u53f7\uff0c \uff1a\u4e0d\u52a0\u7b26\u53f7\uff0c0 \u53f3\u5bf9\u9f50\uff09\u3002\u6307\u5b9a\u5b57\u6bb5\u5bbd\u5ea6\uff1a\u6570\u5b57\u8868\u793a\u5143\u7d20\u5360\u7528\u7684\u5b57\u7b26\u6570\u3002\u4f7f\u7528 string.format() \u65b9\u6cd5\uff1a\u4f20\u5165\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u8981\u6253\u5370\u7684\u5143\u7d20\u3002 Java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4 \u5728 Java \u4e2d\u5bf9\u9f50\u6253\u5370\u7c7b\u6570\u7ec4\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 String.format() \u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u6307\u5b9a\u5143\u7d20\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u5bf9\u9f50\u65b9\u5f0f\u3002 \u6b65\u9aa4\uff1a \u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u521b\u5efa\u4e00\u4e2a\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u8868\u793a\u5143\u7d20\u7684\u5360\u4f4d\u7b26\uff08\u4f8b\u5982\uff0c&#8221;%s&#8221; \u7528\u4e8e\u5b57\u7b26\u4e32\uff09\u3002 \u4f7f\u7528 % \u7b26\u53f7\u6307\u5b9a\u5360\u4f4d\u7b26\uff0c\u540e\u8ddf\u4e00\u4e2a\u5b57\u6bcd\u8868\u793a\u5bf9\u9f50\u65b9\u5f0f\u548c\u5b57\u6bb5\u5bbd\u5ea6\u3002 \u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\uff1a \u4f7f\u7528\u4ee5\u4e0b\u5b57\u6bcd\u6307\u5b9a\u5bf9\u9f50\u65b9\u5f0f\uff1a -\uff1a\u5de6\u5bf9\u9f50 +\uff1a\u5728\u6570\u5b57\u524d\u52a0\u6b63\u8d1f\u53f7 \uff1a\u6570\u5b57\u4e0d\u52a0\u7b26\u53f7\uff08\u65e0\u7b26\u53f7\u586b\u5145\uff09 0\uff1a\u53f3\u5bf9\u9f50\uff08\u7528\u96f6\u586b\u5145\uff09 \u6307\u5b9a\u5b57\u6bb5\u5bbd\u5ea6\uff1a \u5728\u5bf9\u9f50\u5b57\u6bcd\u540e\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u5b57\u6bb5\u5bbd\u5ea6\u3002\u8fd9\u5c06\u63a7\u5236\u5143\u7d20\u5728\u8f93\u51fa\u4e2d\u5360\u7528\u7684\u5b57\u7b26\u6570\u3002 \u4f7f\u7528 String.format() \u65b9\u6cd5\uff1a \u4f7f\u7528 String.format() \u65b9\u6cd5\uff0c\u4f20\u5165\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u548c\u8981\u6253\u5370\u7684\u5143\u7d20\u3002 \u793a\u4f8b\uff1a \u4ee5\u4e0b\u793a\u4f8b\u5bf9\u9f50\u6253\u5370\u4e00\u4e2a String[] \u6570\u7ec4\uff1a String[] names = {&#8220;John&#8221;, &#8220;Mary&#8221;, &#8220;Bob&#8221;}; \/\/ \u521b\u5efa\u683c\u5f0f\u5316\u5b57\u7b26\u4e32 String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-65439","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65439","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=65439"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/65439\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=65439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=65439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=65439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}