Java String Trim Right rtrim(String s)

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

Description

Trims the space characters from the end of a string.

License

Open Source License

Parameter

Parameter Description
s the string to edit

Return

the trimmed string

Declaration

public static final String rtrim(String s) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  ww w  . ja va  2  s.c  om*/
     * Trims the space characters from the end of a string.
     * For example, the call <CODE>rtrim ("Tennessee", 'e')</CODE>
     * returns the string "Tenness".<BR>
     * All characters that have codes less than or equal to
     * <code>'&#92;u0020'</code> (the space character) are considered to be
     * white space.
     * @param s the string to edit
     * @return the trimmed string
     */
    public static final String rtrim(String s) {
        int count = s.length();
        int len = count;
        while ((0 < len) && isSpace(s.charAt(len - 1))) {
            len--;
        }
        return (len < count) ? s.substring(0, len) : s;
    }

    public static final boolean isSpace(String s) {
        if (s != null) {
            int len = s.length();
            for (int i = 0; i < len; i++) {
                char c = s.charAt(i);
                if (c != ' ' && c != '\t' && c != '\n' && c != '\r') {
                    return false;
                }
            }
        }
        return true;
    }

    private static boolean isSpace(char c) {
        return c <= ' ';
    }
}

Related

  1. rTrim(final String value)
  2. rtrim(String input)
  3. rTrim(String orgStr, String delimiter)
  4. rtrim(String orig)
  5. rtrim(String pString)
  6. rtrim(String s)
  7. rtrim(String s)
  8. rTrim(String s)
  9. rtrim(String s)