Java String Trim Left leftTrim(final String input)

Here you can find the source of leftTrim(final String input)

Description

Get a version of a String with all leading whitespace removed.

License

Open Source License

Parameter

Parameter Description
input a parameter

Return

String with leading whitespace removed. Returns the original reference, if there is no leading whitespace.

Declaration

public static final String leftTrim(final String input) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w w  .j a v  a2 s.  c  o  m
     * Get a version of a String with all leading whitespace removed.
     * 
     * @param input
     * @return String with leading whitespace removed. Returns the original
     *         reference, if there is no leading whitespace.
     */
    public static final String leftTrim(final String input) {
        if (input == null) {
            return null;
        }
        final int len = input.length();
        int beginIndex = 0;
        for (int i = 0; i < len; i++) {
            if (Character.isWhitespace(input.charAt(i))) {
                ++beginIndex;
            } else {
                break;
            }
        }
        if (beginIndex > 0) {
            if (beginIndex >= len) {
                return "";
            } else {
                return input.substring(beginIndex);
            }
        } else {
            return input;
        }
    }
}

Related

  1. leftAndRightTrim(String input, char charToTrim)
  2. leftTrim(char[] text, int offset)
  3. leftTrim(final String aString)
  4. leftTrim(final String input, final char charToTrim)
  5. leftTrim(String input, char charToTrim)
  6. leftTrim(String rawString)
  7. leftTrim(String str)