Java String Trim Left ltrim(String str, String defaultValue)

Here you can find the source of ltrim(String str, String defaultValue)

Description

This function returns a string with whitespace stripped from the beginning of str

License

LGPL

Parameter

Parameter Description
str String to clean

Return

cleaned String

Declaration

public static String ltrim(String str, String defaultValue) 

Method Source Code

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

public class Main {
    /** /* www  . j a  v  a 2  s . c  o  m*/
      * This function returns a string with whitespace stripped from the beginning of str 
      * @param str String to clean 
      * @return cleaned String 
      */
    public static String ltrim(String str, String defaultValue) {
        if (str == null)
            return defaultValue;
        int len = str.length();
        int st = 0;

        while ((st < len) && (str.charAt(st) <= ' ')) {
            st++;
        }
        return ((st > 0)) ? str.substring(st) : str;
    }

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

    /**
     * this method works different from the regular substring method, the regular substring method takes startIndex and endIndex as second and third argument,
     * this method takes offset and length
     * @param str
     * @param off
     * @param len
     * @return
     */
    public static String substring(String str, int off, int len) {
        return str.substring(off, off + len);
    }
}

Related

  1. ltrim(String str)
  2. ltrim(String str)
  3. ltrim(String str)
  4. ltrim(String str)
  5. ltrim(String str, String charList)
  6. ltrim(String text, char c)
  7. ltrimCount(final String input)
  8. ltrimNewline(String s)
  9. ltrimZero(String inputString)