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

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

Description

lpad

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w w w.  j a  v a2 s . c  o  m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

public class Main {
    public static String lpad(String s, String fill, int len) {
        if (s == null || fill == null || s.length() >= len || fill.length() == 0) {
            return s;
        }
        StringBuffer buf = new StringBuffer(s);
        while (buf.length() < len) {
            buf.insert(0, fill);
        }
        return buf.substring(buf.length() - len, buf.length());
    }

    public static int length(String s) {
        if (s == null) {
            return 0;
        } else {
            return s.length();
        }
    }

    public static String substring(String s, int start) {
        if (s == null || start >= s.length()) {
            return "";
        }
        return s.substring(start);
    }

    public static String substring(String s, int start, int len) {
        if (s == null || start >= s.length()) {
            return "";
        }
        len = Math.min(s.length() - start, len);
        return s.substring(start, start + len);
    }
}

Related

  1. lpad(String s, char addChar, int length)
  2. lpad(String s, int len, char ch)
  3. lpad(String s, int len, String padc)
  4. lpad(String s, int length, char pad)
  5. lpad(String s, int width)
  6. lpad(String src, int length, char padding)
  7. lpad(String str, char pad, int len)
  8. lPad(String str, int len, char pad)
  9. lpad(String str, int len, char padding)