Java String Trim Right rtrim(String str, String defaultValue)

Here you can find the source of rtrim(String str, String defaultValue)

Description

This function returns a string with whitespace stripped from the end of str

License

LGPL

Parameter

Parameter Description
str String to clean

Return

cleaned String

Declaration

public static String rtrim(String str, String defaultValue) 

Method Source Code

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

public class Main {
    /** //ww w. j ava2s  . c  om
     * This function returns a string with whitespace stripped from the end of str 
     * @param str String to clean 
     * @return cleaned String 
     */
    public static String rtrim(String str, String defaultValue) {
        if (str == null)
            return defaultValue;
        int len = str.length();

        while ((0 < len) && (str.charAt(len - 1) <= ' ')) {
            len--;
        }
        return (len < str.length()) ? str.substring(0, len) : str;
    }

    public static int length(String str) {
        if (str == null)
            return 0;
        return str.length();
    }

    /**
     * this method works different from the regular substring method, the regular substring method takes startIndex and endIndex as second and third argument,
     * this method takes offset and length
     * @param str
     * @param off
     * @param len
     * @return
     */
    public static String substring(String str, int off, int len) {
        return str.substring(off, off + len);
    }
}

Related

  1. rtrim(String str)
  2. rtrim(String str)
  3. rtrim(String str)
  4. rtrim(String str)
  5. rtrim(String str, String charList)
  6. rtrim(String str, String suffixs)
  7. rtrim(String string, String end)
  8. rtrim(String text, char c)
  9. rTrim(String value)