Android String Trim trimStart(String s)

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

Description

Trim characters from only the beginning of a string.

License

Apache License

Parameter

Parameter Description
s String to be trimmed

Return

String with whitespace characters removed from the beginning

Declaration

public static String trimStart(String s) 

Method Source Code

//package com.java2s;
/**/* www .  j  av a2 s.  c o m*/
 * Copyright (c) 2000, Google Inc.
 *
 * 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 {
    /**
     * Trim characters from only the beginning of a string.
     * This is a convenience method, it simply calls trimStart(s, null).
     *
     * @param s String to be trimmed
     * @return String with whitespace characters removed from the beginning
     */
    public static String trimStart(String s) {
        return trimStart(s, null);
    }

    /**
     * Trim characters from only the beginning of a string.
     * This method will remove all whitespace characters
     * (defined by Character.isWhitespace(char), in addition to the characters
     * provided, from the end of the provided string.
     *
     * @param s String to be trimmed
     * @param extraChars Characters in addition to whitespace characters that
     *                   should be trimmed.  May be null.
     * @return String with whitespace and characters in extraChars removed
     *                   from the beginning
     */
    public static String trimStart(String s, String extraChars) {
        int trimCount = 0;
        while (trimCount < s.length()) {
            char ch = s.charAt(trimCount);
            if (Character.isWhitespace(ch)
                    || (extraChars != null && extraChars.indexOf(ch) >= 0)) {
                trimCount++;
            } else {
                break;
            }
        }

        if (trimCount == 0) {
            return s;
        }
        return s.substring(trimCount);
    }
}

Related

  1. trim(String str)
  2. trimToNull(String str)
  3. splitAndTrim(String str, String delims)
  4. trimEnd(String s)