Java String Trim Left ltrim(String s)

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

Description

Does left trim.

License

Apache License

Parameter

Parameter Description
s the string to trim

Return

the string left trimmed (equivalent to but only on the left. Handles null value properly.

Declaration

public static String ltrim(String s) 

Method Source Code

//package com.java2s;
/*/*from w w w  .ja v  a2  s.  co m*/
 * Copyright (c) 2012 Yan Pujante
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    /**
     * Does left trim. The code is inpsired by the {@link String#trim()} code, but it cannot
     * be as efficient...
     *
     * @param s the string to trim
     * @return the string left trimmed (equivalent to {@link String#trim()} but only on the left.
     * Handles <code>null</code> value properly.
     */
    public static String ltrim(String s) {
        if (s == null)
            return null;

        int len = s.length();
        int st = 0;

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

Related

  1. ltrim(String s)
  2. lTrim(String s)
  3. ltrim(String s)
  4. ltrim(String s)
  5. ltrim(String s)
  6. ltrim(String s)
  7. ltrim(String s, char character)
  8. ltrim(String s, Character c)
  9. ltrim(String source)