Java String Trim Right rightTrim(String str)

Here you can find the source of rightTrim(String str)

Description

Removes trailing whitespaces.

License

Open Source License

Parameter

Parameter Description
str String to treat

Return

str without trailing whitespaces

Declaration

public static String rightTrim(String str) 

Method Source Code

//package com.java2s;
/*/*w w w .j  a  v  a 2s.com*/
 *  File: MiscUtil.java 
 *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
 *  A commercial license is available, see http://www.jaret.de.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

public class Main {
    /**
     * Removes trailing whitespaces.
     * 
     * @param str String to treat
     * @return str without trailing whitespaces
     */
    public static String rightTrim(String str) {
        if (str.length() == 0) {
            return str;
        }
        int i = str.length() - 1;
        while (i >= 0) {
            char c = str.charAt(i);
            if (Character.isWhitespace(c)) {
                i--;
            } else {
                break;
            }
        }
        return str.substring(0, i + 1);
    }
}

Related

  1. rightTrim(String s)
  2. rightTrim(String s, char c)
  3. rightTrim(String src)
  4. rightTrim(String str)
  5. rightTrim(String str)
  6. rightTrim(String string)
  7. rightTrim(String value)
  8. rightTrim(String[] values)
  9. rightTrim(StringBuilder pStringBuilder)