Java String Left left(String str, int length)

Here you can find the source of left(String str, int length)

Description

left of string

License

Apache License

Parameter

Parameter Description
str a parameter
length a parameter

Declaration

public static String left(String str, int length) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from   w w  w .j  av  a 2 s  . c om*/
     * left of string 
     * @param str
     * @param length
     * @return
     */
    public static String left(String str, int length) {
        String rc = NVL(str);
        if (rc.length() <= length) {
            return str;
        }
        return rc.substring(0, length);
    }

    /**
     * NVL - fix null value
     * @param String value
     * @param replace with 
     * @return
     */
    public static String NVL(String str, String replace) {
        if (str == null || str.trim().equals("") || str.trim().toUpperCase().equals("NULL"))
            return replace;
        return str;
    }

    /**
     * NVL - fix null value 
     * @param string value 
     * @return
     */
    public static String NVL(String str) {
        return NVL(str, "");
    }

    /**
     * substring 
     * 
     * @param text
     * @param startPos
     * @param length
     * @return
     */
    public static String substring(String text, int startPos, int endPos) {
        String str = NVL(text);
        // enough length? 
        str = fill(str, endPos);
        // get substr 
        return str.substring(startPos, endPos).trim();
    }

    /**
     * fill
     * 
     * @param value
     * @param length
     * @param fillChar
     * @param isLeadingFill
     * @return
     */
    public static String fill(String value, int length, char fillChar, boolean isLeadingFill) {
        value = NVL(value);
        String rc = value;

        for (int i = 0; i < length - value.length(); i++) {
            if (isLeadingFill) {
                rc = fillChar + rc;
            } else {
                rc += fillChar;
            }
        }
        return rc;
    }

    /**
     * fill 
     * 
     * @param value
     * @param length
     * @param fillChar
     * @return
     */
    public static String fill(String value, int length, char fillChar) {
        return fill(value, length, fillChar, false);
    }

    /**
     * fill
     * 
     * @param value
     * @param length
     * @param isLeadingFill
     * @return
     */
    public static String fill(String value, int length, boolean isLeadingFill) {
        return fill(value, length, ' ', isLeadingFill);
    }

    /**
     * fill
     * 
     * @param value
     * @param length
     * @return
     */
    public static String fill(String value, int length) {
        return fill(value, length, ' ', false);
    }
}

Related

  1. left(String str, int len)
  2. left(String str, int len)
  3. left(String str, int len)
  4. left(String str, int len)
  5. left(String str, int len, String appendStrIfOver)
  6. left(String str, int size)
  7. left(String string, int length)
  8. left(String text, int len)
  9. left(String text, int max)