Java String Unpad unPadRight(String s, char c)

Here you can find the source of unPadRight(String s, char c)

Description

Unpad from right.

License

Open Source License

Parameter

Parameter Description
s - original string
c - padding char

Return

unPadded string.

Declaration

public static String unPadRight(String s, char c) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  ww. j  a  va2 s . c om*/
     * Unpad from right. In case the string to be returned is empty, the result
     * is c
     * 
     * @param s
     *            - original string
     * @param c
     *            - padding char
     * @return unPadded string.
     */
    public static String unPadRight(String s, char c) {
        if ((s.trim().length() == 0) && (c == ' '))
            return Character.toString(c);
        else if ((s.trim().length() == 0))
            return s;
        String sTrim = s.trim();
        int end = sTrim.length();
        while ((0 < end) && (sTrim.charAt(end - 1) == c))
            end--;
        return (0 < end) ? sTrim.substring(0, end) : sTrim.substring(0, 1);
    }
}

Related

  1. unPad(byte[] bytes)
  2. unPad(byte[] src, int start, byte[] dest)
  3. unPadLeft(String s, char c)
  4. unpadZeroString(String string)