{"id":921,"date":"2018-09-17T18:25:40","date_gmt":"2018-09-17T09:25:40","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=921"},"modified":"2018-12-23T02:00:59","modified_gmt":"2018-12-22T17:00:59","slug":"string-lpad-rpad","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/zh_tw\/\u90e8\u843d\u683c\/java\/\u5b57\u4e32-lpad-rpad","title":{"rendered":"\u4f7f\u7528 Java LPAD\u3001RPAD \u9032\u884c\u683c\u5f0f\u5316"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">\u4f7f\u7528 Java LPAD\u3001RPAD \u9032\u884c\u683c\u5f0f\u5316\u3002\u5728\u5b57\u4e32\u7684\u5de6\u5074\u548c\u53f3\u5074\u586b\u5145\u6307\u5b9a\u5b57\u7b26<\/a><\/h2>\n<p>\n<strong>Java<\/strong>\u7136\u5f8c\uff0c\u60a8\u53ef\u4ee5\u5c0d\u5b57\u4e32\u4f7f\u7528 SQL \u51fd\u6578\u3002<strong>LPAD<\/strong>,,<strong>RPAD<\/strong>\u9019\u662f\u57f7\u884c\u76f8\u540c\u904e\u7a0b\u7684\u7bc4\u4f8b\u4f86\u6e90\u3002<br \/>\n\u9019\u4e9b\u90fd\u662f<strong>\u56fa\u5b9a\u9577\u5ea6\u683c\u5f0f<\/strong>\u8655\u7406\u4e8b\u60c5\u7684\u6642\u5019\u975e\u5e38\u6709\u7528\u3002\n<\/p>\n<ul>\n<li><strong>LPAD<\/strong>...\u50b3\u56de\u5728\u5b57\u4e32\u5de6\u5074\u586b\u5165\u91cd\u8907\u5b57\u5143\u7684\u5b57\u4e32\uff0c\u4f7f\u5b57\u5143\u7e3d\u6578\u9054\u5230\u6307\u5b9a\u7684\u4f4d\u6578\u3002<\/li>\n<li><strong>RPAD<\/strong>...\u586b\u5165\u4e26\u50b3\u56de\u5b57\u4e32\u53f3\u5074\u7684\u91cd\u8907\u5b57\u4e32\uff0c\u4f7f\u5b57\u5143\u7e3d\u6578\u9054\u5230\u6307\u5b9a\u7684\u4f4d\u6578\u3002<\/li>\n<\/ul>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"1\">\u7bc4\u4f8b\u7a0b\u5f0f<\/a><\/h2>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * LPAD\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n * \u6587\u5b57\u5217[str]\u306e\u5de6\u306b\u6307\u5b9a\u3057\u305f\u6587\u5b57\u5217[addStr]\u3092[len]\u306b<br \/>\n * \u6e80\u305f\u3059\u307e\u3067\u633f\u5165\u3057\u307e\u3059\u3002<br \/>\n * @param str \u5bfe\u8c61\u6587\u5b57\u5217<br \/>\n * @param len \u88dc\u5145\u3059\u308b\u307e\u3067\u306e\u6841\u6570\uff08LPAD\u3092\u884c\u3063\u305f\u5f8c\u306e\u30b5\u30a4\u30ba\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\uff09<br \/>\n * @param addStr \u633f\u5165\u3059\u308b\u6587\u5b57\u5217<br \/>\n * @return \u5909\u63db\u5f8c\u306e\u6587\u5b57\u5217\u3002<br \/>\n *\/<br \/>\npublic static String lpad(String str, int len, String addStr) {<br \/>\n    return fillString(str, &#8220;L&#8221;, len, addStr);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u57f7\u884c RPAD\u3002<br \/>\n * \u5c07\u5b57\u4e32[str]\u53f3\u5074\u7684\u6307\u5b9a\u5b57\u4e32[addStr]\u52a0\u5165\u5230[len]<br \/>\n * \u63d2\u5165\u76f4\u81f3\u6eff\u3002<br \/>\n * @param str \u76ee\u6a19\u5b57\u4e32<br \/>\n * @param len \u8981\u91cd\u65b0\u586b\u5165\u7684\u4f4d\u6578\uff08\u6307\u5b9a\u57f7\u884c RPAD \u5f8c\u7684\u5927\u5c0f\uff09<br \/>\n * @param addStr \u8981\u63d2\u5165\u7684\u5b57\u4e32<br \/>\n * @return \u8f49\u63db\u5f8c\u7684\u5b57\u4e32\u3002<br \/>\n *\/<br \/>\n\u516c\u5171\u975c\u614b\u5b57\u4e32rpad\uff08\u5b57\u4e32str\uff0cint len\uff0c\u5b57\u4e32addStr\uff09{<br \/>\n    \u8fd4\u56de fillString(str, \u201cR\u201d, len, addStr);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \u5c07\u8981\u65b0\u589e\u7684\u5b57\u4e32[addStr]\u52a0\u5165\u5230\u5b57\u4e32[str]\u4e2d<br \/>\n * \u5728[\u4f4d\u7f6e]\u8655\u63d2\u5165\uff0c\u76f4\u5230[\u9577\u5ea6]\u586b\u6eff\u3002<br \/>\n *<br \/>\n * *\u5373\u4f7f [str] \u70ba null \u6216\u7a7a\u6587\u5b57\uff0c\u4e5f\u4f7f\u7528 [addStr]<br \/>\n * \u50b3\u56de\u63d2\u5165\u7684\u7d50\u679c\uff0c\u76f4\u5230[len]\u586b\u6eff\u3002<br \/>\n * @param str \u76ee\u6a19\u5b57\u4e32<br \/>\n * @param \u4f4d\u7f6e \u5728 \u21d2 L \u6216 l \u4e4b\u524d\u63d2\u5165 \u5728 \u21d2 R \u6216 r \u4e4b\u5f8c\u63d2\u5165<br \/>\n * @param len \u8981\u88dc\u5145\u7684\u4f4d\u6578<br \/>\n * @param addStr \u8981\u63d2\u5165\u7684\u5b57\u4e32<br \/>\n * @return \u8f49\u63db\u5f8c\u7684\u5b57\u4e32\u3002<br \/>\n *\/<br \/>\n\u516c\u7528\u975c\u614b\u5b57\u4e32 fillString(\u5b57\u4e32 str, \u5b57\u4e32\u4f4d\u7f6e,<br \/>\n        \u570b\u969b\u9577\u5ea6\uff0c<br \/>\n        \u5b57\u4e32 addStr) {<br \/>\n    if (addStr == null || addStr.length() == 0) {<br \/>\n        \u62cb\u51fa\u65b0\u7684 IllegalArgumentException<br \/>\n            (\u201c\u8981\u63d2\u5165\u7684\u5b57\u4e32\u7684\u503c\u7121\u6548\u3002addStr=\u201d+addStr);<br \/>\n    }<br \/>\n    if (str == null) {<br \/>\n        \u5b57\u4e32=\u201c\u201d\uff1b<br \/>\n    }<br \/>\n    StringBuffer \u7de9\u885d\u5340 = new StringBuffer(str);<br \/>\n    while (len &gt; buffer.length()) {<br \/>\n        if (position.equalsIgnoreCase(\u201cl\u201d)) {<br \/>\n            int sum = buffer.length() + addStr.length();<br \/>\n            \u5982\u679c\uff08\u7e3d\u548c&gt;\u9577\u5ea6\uff09{<br \/>\n                addStr = addStr.\u5b50\u5b57\u4e32<br \/>\n                    (0,addStr.length() \u2013 (sum \u2013 len));<br \/>\n                buffer.insert(0, addStr);<br \/>\n            }\u5225\u7684{<br \/>\n                buffer.insert(0, addStr);<br \/>\n            }<br \/>\n        } \u5225\u7684 {<br \/>\n            buffer.append(addStr);<br \/>\n        }<br \/>\n    }<br \/>\n    if (buffer.length() == len) {<br \/>\n        \u8fd4\u56de buffer.toString();<br \/>\n    }<br \/>\n    \u8fd4\u56de buffer.toString().substring(0, len);<br \/>\n}<br \/>\n<\/textarea><br \/>\n<\/p>\n<h2 class=\"common_title\"><a name=\"2\">\u57f7\u884c\u7d50\u679c<\/a><\/h2>\n<p>\u25c6\u57f7\u884c\u7bc4\u4f8b<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) { \/\/ \u5c0d\u65bc lpad\u3002\u586b\u5beb\u5de6\u908a\u7684\u7a7a\u683c\uff0c\u76f4\u5230\u9054\u5230 5 \u4f4d\u6578\u5b57\u3002 String ret = lpad(&quot;abc&quot;, 5 , &quot; &quot;); System.out.println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); \/\/ \u5c0d\u65bc rpad\u3002\u586b\u5beb\u53f3\u5074\u7684\u7a7a\u683c\uff0c\u76f4\u5230\u9054\u5230 5 \u4f4d\u6578\u5b57\u3002 ret = rpad(&quot;abc&quot;, 5 , &quot; &quot;); System.out.println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); \/\/ \u82e5\u70banull ret = rpad(null, 5 , &quot; &quot;); System. out .println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); }\r\n<\/pre>\n<p>\u25c6\u8f38\u51fa\u7d50\u679c<\/p>\n<pre class=\"console\">&#039;abc&#039;&#039;abc&#039;&#039;&#039;\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Java LPAD\u3001RPAD \u9032\u884c\u683c\u5f0f\u5316\u3002\u5728\u5b57\u4e32\u7684\u5de6\u53f3\u5169\u5074\u586b\u5165\u6307\u5b9a\u5b57\u5143\u5728Java\u4e2d\uff0c\u4f7f\u7528SQL\u51fd\u6578L [...]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/posts\/921"}],"collection":[{"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/comments?post=921"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/posts\/921\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/zh_tw\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}