Java String Pad Left lpad(String s, int len, String padc)

Here you can find the source of lpad(String s, int len, String padc)

Description

lpad

License

Apache License

Declaration

public static String lpad(String s, int len, String padc) 

Method Source Code

//package com.java2s;
/*/*from ww w . ja  v a 2s .c om*/
 *    Copyright 2012 The MyBatisORM Team
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

public class Main {
    public static String lpad(String s, int len, String padc) {
        int slen = 0;
        if (s != null) {
            slen = s.length();
            if (slen >= len) {
                return s;
            }
        }
        StringBuilder sb = new StringBuilder(s);
        for (int i = slen; i < len; i++) {
            sb.insert(0, padc);
        }
        return sb.toString();
    }
}

Related

  1. lpad(String input, int length)
  2. lpad(String input, String padChar, int finalLength)
  3. lPad(String input, String replace, int length)
  4. lpad(String s, char addChar, int length)
  5. lpad(String s, int len, char ch)
  6. lpad(String s, int length, char pad)
  7. lpad(String s, int width)
  8. lpad(String s, String fill, int len)
  9. lpad(String src, int length, char padding)