Here you can find the source of left(String str, int length)
Parameter | Description |
---|---|
str | a parameter |
length | a parameter |
public static String left(String str, int length)
//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); } }