Java String Trim Left leftAndRightTrim(String input, char charToTrim)

Here you can find the source of leftAndRightTrim(String input, char charToTrim)

Description

Removes the occurrences of the passed char in the start and end of the string.

License

Common Public License

Declaration

public static String leftAndRightTrim(String input, char charToTrim) 

Method Source Code

//package com.java2s;
/*//from   ww  w.  j  a  v  a  2s.  com
 * @author Fabio Zadrozny
 * Created: June 2005
 * License: Common Public License v1.0
 */

public class Main {
    /**
     * Removes the occurrences of the passed char in the start and end of the
     * string.
     */
    public static String leftAndRightTrim(String input, char charToTrim) {
        return rightTrim(leftTrim(input, charToTrim), charToTrim);
    }

    /**
     * Removes whitespaces at the beggining of the string.
     */
    public static String rightTrim(String input) {
        int len = input.length();
        int st = 0;
        int off = 0;
        char[] val = input.toCharArray();

        while ((st < len) && (val[off + len - 1] <= ' ')) {
            len--;
        }
        return input.substring(0, len);
    }

    /**
     * Removes the occurrences of the passed char in the beggining of the
     * string.
     */
    public static String rightTrim(String input, char charToTrim) {
        int len = input.length();
        int st = 0;
        int off = 0;
        char[] val = input.toCharArray();

        while ((st < len) && (val[off + len - 1] == charToTrim)) {
            len--;
        }
        return input.substring(0, len);
    }

    /**
     * Removes whitespaces at the end of the string.
     */
    public static String leftTrim(String input) {
        int len = input.length();
        int off = 0;
        char[] val = input.toCharArray();

        while ((off < len) && (val[off] <= ' ')) {
            off++;
        }
        return input.substring(off, len);
    }

    /**
     * Removes the occurrences of the passed char in the end of the string.
     */
    public static String leftTrim(String input, char charToTrim) {
        int len = input.length();
        int off = 0;
        char[] val = input.toCharArray();

        while ((off < len) && (val[off] == charToTrim)) {
            off++;
        }
        return input.substring(off, len);
    }
}

Related

  1. leftTrim(char[] text, int offset)
  2. leftTrim(final String aString)
  3. leftTrim(final String input)
  4. leftTrim(final String input, final char charToTrim)