{"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\/en\/blog\/java\/string-lpad-rpad","title":{"rendered":"Shaped by Java LPAD and RPAD"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Formatting with Java LPAD and RPAD. Fill in the specified characters on the left and right of the string.<\/a><\/h2>\n<p>\n<strong>Java<\/strong>and then use the SQL function<strong>LPAD<\/strong>, ,<strong>RPAD<\/strong>Here is a sample source that performs the same process as<br \/>\nThese are<strong>fixed length format<\/strong>This is very useful when dealing with\n<\/p>\n<ul>\n<li><strong>LPAD<\/strong>... Returns a string with a repeated string filled in on the left side of the string so that the total number of characters is the specified number of digits.<\/li>\n<li><strong>RPAD<\/strong>... Returns a string with a repeated string filled in on the right side of the string so that the total number of characters is the specified number of digits.<\/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\">sample program<\/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 * Perform RPAD.<br \/>\n * Add the specified string [addStr] to the right of the string [str] to [len]<br \/>\n * Insert until full.<br \/>\n * @param str target string<br \/>\n * @param len Number of digits to refill (specifies the size after performing RPAD)<br \/>\n * @param addStr String to insert<br \/>\n * @return The string after conversion.<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 * Add the string [addStr] to be added to the string [str]<br \/>\n * Insert at [position] until [len] is filled.<br \/>\n *<br \/>\n * *Even if [str] is null or an empty literal, use [addStr]<br \/>\n * Returns the result inserted until [len] is filled.<br \/>\n * @param str target string<br \/>\n * @param position Insert before \u21d2 L or l Insert after \u21d2 R or r<br \/>\n * @param len Number of digits to replenish<br \/>\n * @param addStr String to insert<br \/>\n * @return The string after conversion.<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            (\u201cThe value of the string to be inserted is invalid. 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(\u201cl\u201d)) {<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\">Execution Result<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">public static void main(String[] args) {\r\n    \/\/ For lpad, fill in the spaces to the left until there are 5 digits.\r\n    String ret = lpad(\"abc\", 5 , \" \");\r\n    System.out.println(\"'\" + ret + \"'\");\r\n\r\n    \/\/ For rpad. fill space to the right until 5 digits.\r\n    ret = rpad(\"abc\", 5 , \" \");\r\n    System.out.println(\"'\" + ret + \"'\");\r\n\r\n    \/\/ If null\r\n    ret = rpad(null, 5 , \" \");\r\n    System.out.println(\"'\" + ret + \"'\"); \/\/ if null\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<pre class=\"console\">'abc'\r\n' 'abc '\r\n' '\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>Formatted with Java LPAD, RPAD. Fill specified characters on the left and right sides of a string In Java, use the SQL function 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\/en\/wp-json\/wp\/v2\/posts\/921"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=921"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/921\/revisions\/1892"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}