Java String Trim Left ltrim(String pString)

Here you can find the source of ltrim(String pString)

Description

Trims the argument string for whitespace on the left side only.

License

Open Source License

Parameter

Parameter Description
pString the string to trim

Return

the string with no whitespace on the left, or null if the string argument is null .

Declaration

public static String ltrim(String pString) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w  w  w. j  a  va2s.c  o m
     * Trims the argument string for whitespace on the left side only.
     *
     * @param pString the string to trim
     * @return the string with no whitespace on the left, or {@code null} if
     *         the string argument is {@code null}.
     * @see #rtrim
     * @see String#trim()
     */
    public static String ltrim(String pString) {
        if ((pString == null) || (pString.length() == 0)) {
            return pString;// Null or empty string
        }
        for (int i = 0; i < pString.length(); i++) {
            if (!Character.isWhitespace(pString.charAt(i))) {
                if (i == 0) {
                    return pString;// First char is not whitespace
                } else {
                    return pString.substring(i);// Return rest after whitespace
                }
            }
        }

        // If all whitespace, return empty string
        return "";
    }

    /**
     * Gets the first substring between the given string boundaries.
     * <p/>
     *
     * @param pSource              The source string.
     * @param pBeginBoundaryString The string that marks the beginning.
     * @param pEndBoundaryString   The string that marks the end.
     * @param pOffset              The index to start searching in the source
     *                             string. If it is less than 0, the index will be set to 0.
     * @return the substring demarcated by the given string boundaries or null
     *         if not both string boundaries are found.
     */
    public static String substring(final String pSource, final String pBeginBoundaryString,
            final String pEndBoundaryString, final int pOffset) {
        // Check offset
        int offset = (pOffset < 0) ? 0 : pOffset;

        // Find the start index
        int startIndex = pSource.indexOf(pBeginBoundaryString, offset) + pBeginBoundaryString.length();

        if (startIndex < 0) {
            return null;
        }

        // Find the end index
        int endIndex = pSource.indexOf(pEndBoundaryString, startIndex);

        if (endIndex < 0) {
            return null;
        }
        return pSource.substring(startIndex, endIndex);
    }
}

Related

  1. ltrim(final String str, final char filler)
  2. ltrim(final String text)
  3. lTrim(final String value)
  4. ltrim(String list, char[] del)
  5. ltrim(String orig)
  6. ltrim(String s)
  7. ltrim(String s)
  8. ltrim(String s)
  9. ltrim(String s)