Java String Trim Right rtrim(String text, char c)

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

Description

rtrim

License

Apache License

Parameter

Parameter Description
text a parameter
c a parameter

Return

String

Declaration

public static String rtrim(String text, char c) 

Method Source Code

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

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

    /**//from   www .j a  v  a2  s.co m
     * @param text
     * @param c
     * @return String
     */
    public static String rtrim(String text, char c) {
        if (text == null) {
            return EMPTY;
        }

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

        while ((i < j) && (text.charAt(j - 1) == c)) {
            j--;
        }
        return (j < text.length()) ? text.substring(i, j) : 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. rtrim(String str)
  2. rtrim(String str, String charList)
  3. rtrim(String str, String defaultValue)
  4. rtrim(String str, String suffixs)
  5. rtrim(String string, String end)
  6. rTrim(String value)
  7. rtrim(String value, int maxLength)
  8. rTrimObj(Object o)