Java String Sub String substring(String text, int startPos, int endPos)

Here you can find the source of substring(String text, int startPos, int endPos)

Description

substring

License

Apache License

Parameter

Parameter Description
text a parameter
startPos a parameter
length a parameter

Declaration

public static String substring(String text, int startPos, int endPos) 

Method Source Code

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

public class Main {
    /**/*w  w w . j  av  a2 s .  co m*/
     * 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();
    }

    /**
     * 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, "");
    }

    /**
     * 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. substring(String string, String beginning, String end)
  2. subString(String string, String inizio, String fine)
  3. substring(String subject, int start, int end)
  4. substring(String text, int begin, int end)
  5. substring(String text, int start, int end)
  6. substring(String text, int startPos, int len)
  7. subString(String text, String leadingPart)
  8. subString(String val, int start, int end)
  9. SubString(String x, long y)