{"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\/ko\/blog\/java\/\ubb38\uc790\uc5f4-lpad-rpad","title":{"rendered":"Java LPAD, RPAD\ub85c \uc131\ud615"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java LPAD, RPAD\ub85c \uc131\ud615. \ubb38\uc790\uc5f4\uc758 \uc67c\ucabd\uacfc \uc624\ub978\ucabd\uc5d0 \uc9c0\uc815\ub41c \ubb38\uc790 \ucc44\uc6b0\uae30<\/a><\/h2>\n<p>\n<strong>Java<\/strong>\uadf8\ub798\uc11c \ubb38\uc790\uc5f4\uc5d0 \ub300\ud55c SQL \ud568\uc218<strong>LPAD<\/strong>,,,<strong>RPAD<\/strong>\uc640 \uac19\uc740 \ucc98\ub9ac\ub97c \uc2e4\uc2dc\ud558\ub294 \uc0d8\ud50c \uc18c\uc2a4\ub97c \uc18c\uac1c\ud569\ub2c8\ub2e4.<br \/>\n\uc774\ub4e4\uc740<strong>\uace0\uc815 \uae38\uc774 \ud615\uc2dd<\/strong>\ub2e4\ub8f0 \ub54c \ub9e4\uc6b0 \ud3b8\ub9ac\ud569\ub2c8\ub2e4.\n<\/p>\n<ul>\n<li><strong>LPAD<\/strong>\u00b7\u00b7\u00b7 \uc804\uccb4\uc758 \ubb38\uc790\uc218\uac00 \uc9c0\uc815\uc758 \uc790\ub9ac\uc218\uac00 \ub418\ub3c4\ub85d(\ub4ef\uc774) \uce90\ub9ad\ud130 \ub77c\uc778\uc758 \uc88c\uce21\uc5d0 \ubc18\ubcf5 \uce90\ub9ad\ud130 \ub77c\uc778\uc744 \uba54\uc6b0\uace0 \ub3cc\ub824\uc90d\ub2c8\ub2e4.<\/li>\n<li><strong>RPAD<\/strong>\u00b7\u00b7\u00b7 \uc804\uccb4\uc758 \ubb38\uc790\uc218\uac00 \uc9c0\uc815\uc758 \uc790\ub9ac\uc218\uac00 \ub418\ub3c4\ub85d(\ub4ef\uc774) \uce90\ub9ad\ud130 \ub77c\uc778\uc758 \uc6b0\uce21\uc5d0 \ubc18\ubcf5 \uce90\ub9ad\ud130 \ub77c\uc778\uc744 \uba54\uc6b0\uace0 \ub3cc\ub824\uc90d\ub2c8\ub2e4.<\/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\">\uc0d8\ud50c \ud504\ub85c\uadf8\ub7a8<\/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 *RPAD\ub97c \uc218\ud589\ud569\ub2c8\ub2e4.<br \/>\n * \ubb38\uc790\uc5f4 [str]\uc758 \uc624\ub978\ucabd\uc5d0 \uc9c0\uc815\ub41c \ubb38\uc790\uc5f4 [addStr]\uc744 [len]\ub85c<br \/>\n * \ub9cc\uc871\ud560 \ub54c\uae4c\uc9c0 \uc0bd\uc785\ud569\ub2c8\ub2e4.<br \/>\n * @param str \ub300\uc0c1 \ubb38\uc790\uc5f4<br \/>\n * @param len \ubcf4\ucda9\ud560 \ub54c\uae4c\uc9c0\uc758 \uc790\ub9bf\uc218(RPAD\ub97c \ud55c \ud6c4\uc758 \uc0ac\uc774\uc988\ub97c \uc9c0\uc815\ud569\ub2c8\ub2e4.)<br \/>\n * @param addStr \uc0bd\uc785 \ud560 \ubb38\uc790\uc5f4<br \/>\n * @return \ubcc0\ud658 \ud6c4 \ubb38\uc790\uc5f4.<br \/>\n *\/<br \/>\npublic static String rpad (String str, int len, String addStr) {<br \/>\n    return fillString(str, \u201cR\u201d, len, addStr);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * \ubb38\uc790\uc5f4 [str]\uc5d0 \ub300\ud574 \ubcf4\ucda9 \ud560 \ubb38\uc790\uc5f4 [addStr]<br \/>\n * [position]\uc758 \uc704\uce58\uc5d0 [len]\uc774 \ucda9\uc871\ub420 \ub54c\uae4c\uc9c0 \uc0bd\uc785\ud569\ub2c8\ub2e4.<br \/>\n *<br \/>\n * \u203b [str]\uc774 null\uc774\ub098 \ube48 \ub9ac\ud130\ub7f4 \uc778 \uacbd\uc6b0\uc5d0\ub3c4 [addStr]<br \/>\n * [len]\uc774 \ucda9\uc871\ub420 \ub54c\uae4c\uc9c0 \uc0bd\uc785\ud55c \uacb0\uacfc\ub97c \ubc18\ud658\ud569\ub2c8\ub2e4.<br \/>\n * @param str \ub300\uc0c1 \ubb38\uc790\uc5f4<br \/>\n * @param position \uc55e\uc5d0 \uc0bd\uc785 \u21d2 L or l \ub4a4\uc5d0 \uc0bd\uc785 \u21d2 R or r<br \/>\n * @param len \ubcf4\ucda9\ud558\uae30 \uc804\uc758 \uc790\ub9bf\uc218<br \/>\n * @param addStr \uc0bd\uc785 \ud560 \ubb38\uc790\uc5f4<br \/>\n * @return \ubcc0\ud658 \ud6c4 \ubb38\uc790\uc5f4.<br \/>\n *\/<br \/>\npublic static String fillString(String str, String position,<br \/>\n        int len,<br \/>\n        String addStr) {<br \/>\n    if (addStr == null || addStr.length() == 0) {<br \/>\n        throw new IllegalArgumentException<br \/>\n            (\u201c\uc0bd\uc785\ud560 \ubb38\uc790\uc5f4 \uac12\uc774 \uc798\ubabb\ub418\uc5c8\uc2b5\ub2c8\ub2e4. addStr=\u201d+addStr);<br \/>\n    }<br \/>\n    if (str == null) {<br \/>\n        str = \u201c\u201d;<br \/>\n    }<br \/>\n    StringBuffer buffer = new StringBuffer(str);<br \/>\n    while (len &gt; buffer.length()) {<br \/>\n        if (position.equalsIgnoreCase(&quot;l&quot;)) {<br \/>\n            int sum = buffer.length() + addStr.length();<br \/>\n            if (sum &gt; len) {<br \/>\n                addStr = addStr.substring<br \/>\n                    (0,addStr.length() \u2013 (sum \u2013 len));<br \/>\n                buffer.insert(0, addStr);<br \/>\n            }else{<br \/>\n                buffer.insert(0, addStr);<br \/>\n            }<br \/>\n        } else {<br \/>\n            buffer.append(addStr);<br \/>\n        }<br \/>\n    }<br \/>\n    if (buffer.length() == len) {<br \/>\n        return buffer.toString();<br \/>\n    }<br \/>\n    return buffer.toString().substring(0, len);<br \/>\n}<br \/>\n<\/textarea><br \/>\n<\/p>\n<h2 class=\"common_title\"><a name=\"2\">\uc2e4\ud589 \uacb0\uacfc<\/a><\/h2>\n<p>\u25c6\uc2e4\ud589\uc608<\/p>\n<pre class=\"sample_src\">public static void main (String [] args) { \/\/ lpad\uc758 \uacbd\uc6b0. 5\uc790\ub9ac\uac00 \ub420 \ub54c\uae4c\uc9c0 \uc67c\ucabd\uc5d0 \uacf5\ubc31\uc744 \ucc44\uc6c1\ub2c8\ub2e4. String ret = lpad(&quot;abc&quot;, 5 , &quot; &quot;); System.out.println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); \/\/ rpad\uc758 \uacbd\uc6b0. \ub2e4\uc12f \uc790\ub9ac\uac00 \ub420 \ub54c\uae4c\uc9c0 \uc624\ub978\ucabd\uc73c\ub85c \uacf5\ubc31\uc744 \ucc44\uc6c1\ub2c8\ub2e4. ret = rpad(&quot;abc&quot;, 5 , &quot; &quot;); System.out.println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); \/\/ null\uc758 \uacbd\uc6b0 ret = rpad(null, 5 , &quot; &quot;); System. out.println(&quot;&#039;&quot; + ret + &quot;&#039;&quot;); }\r\n<\/pre>\n<p>\u25c6\ucd9c\ub825 \uacb0\uacfc<\/p>\n<pre class=\"console\">&#039;abc&#039; &#039;abc&#039; &#039;&#039;\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Java LPAD, RPAD\ub85c \uc131\ud615. \uce90\ub9ad\ud130 \ub77c\uc778\uc758 \uc88c\uc6b0\uc5d0 \uc9c0\uc815\ud55c \uce90\ub9ad\ud130\ub97c \uba54\uc6b0\ub294 Java\ub85c, \uce90\ub9ad\ud130 \ub77c\uc778\uc5d0 \ub300\ud55c SQL \ud568\uc218\uc758 L [\u2026]<\/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\/ko\/wp-json\/wp\/v2\/posts\/921"}],"collection":[{"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/comments?post=921"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/posts\/921\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/ko\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}