Java String Trim Right rtrim(String pString)

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

Description

Trims the argument string for whitespace on the right side only.

License

Open Source License

Parameter

Parameter Description
pString the string to trim

Return

the string with no whitespace on the right, or null if the string argument is null .

Declaration

public static String rtrim(String pString) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w  w  w.  j  a v a 2s  .c  om*/
     * Trims the argument string for whitespace on the right side only.
     *
     * @param pString the string to trim
     * @return the string with no whitespace on the right, or {@code null} if
     *         the string argument is {@code null}.
     * @see #ltrim
     * @see String#trim()
     */
    public static String rtrim(String pString) {
        if ((pString == null) || (pString.length() == 0)) {
            return pString;// Null or empty string
        }
        for (int i = pString.length(); i > 0; i--) {
            if (!Character.isWhitespace(pString.charAt(i - 1))) {
                if (i == pString.length()) {
                    return pString;// First char is not whitespace
                } else {
                    return pString.substring(0, i);// Return before whitespace
                }
            }
        }

        // If all whitespace, return empty string
        return "";
    }

    /**
     * Gets the first substring between the given string boundaries.
     * <p/>
     *
     * @param pSource              The source string.
     * @param pBeginBoundaryString The string that marks the beginning.
     * @param pEndBoundaryString   The string that marks the end.
     * @param pOffset              The index to start searching in the source
     *                             string. If it is less than 0, the index will be set to 0.
     * @return the substring demarcated by the given string boundaries or null
     *         if not both string boundaries are found.
     */
    public static String substring(final String pSource, final String pBeginBoundaryString,
            final String pEndBoundaryString, final int pOffset) {
        // Check offset
        int offset = (pOffset < 0) ? 0 : pOffset;

        // Find the start index
        int startIndex = pSource.indexOf(pBeginBoundaryString, offset) + pBeginBoundaryString.length();

        if (startIndex < 0) {
            return null;
        }

        // Find the end index
        int endIndex = pSource.indexOf(pEndBoundaryString, startIndex);

        if (endIndex < 0) {
            return null;
        }
        return pSource.substring(startIndex, endIndex);
    }
}

Related

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