Java String Trim Left ltrim(String text, char c)

Here you can find the source of ltrim(String text, char c)

Description

ltrim

License

Apache License

Parameter

Parameter Description
text a parameter
c a parameter

Return

String

Declaration

public static String ltrim(String text, char c) 

Method Source Code

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

public class Main {
    private static final String EMPTY = "";

    /**/*w  w w  . ja  v a 2  s  .  c  om*/
     * @param text
     * @param c
     * @return String
     */
    public static String ltrim(String text, char c) {
        if (text == null) {
            return EMPTY;
        }

        int i = 0;
        int length = text.length();

        while ((i < length) && (text.charAt(i) == c)) {
            i++;
        }
        return (i > 0 ? text.substring(i) : text);
    }

    /**
     * @param source
     * @param length
     * @param padding
     * @return String
     */
    public static String substring(String source, int length, String padding) {
        if (source == null) {
            return "";
        }

        String s = source.trim();

        char c;
        int size = 0;
        int count = s.length();
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);

            if (c >= 0x0080) {
                size += 2;
                count++;
            } else {
                size++;
            }

            if (size > length) {
                if (c >= 0x4e00) {
                    size -= 2;
                } else {
                    size--;
                }
                break;
            }
            buffer.append(c);
        }

        if (size < count && padding != null) {
            buffer.append(padding);
        }
        return buffer.toString();
    }

    /**
     * @param text
     * @param c
     * @return String
     */
    public static String trim(String text, char c) {
        if (text == null) {
            return EMPTY;
        }

        int i = 0;
        int j = text.length();

        while ((i < j) && (text.charAt(i) == c)) {
            i++;
        }

        while ((i < j) && (text.charAt(j - 1) == c)) {
            j--;
        }
        return ((i > 0) || (j < text.length())) ? text.substring(i, j) : text;
    }
}

Related

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