Java - Write code to Truncate blank spaces at the end of the string provided

Requirements

Write code to Truncate blank spaces at the end of the string provided

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        System.out.println(truncateEndBlanks(str));
    }//from www . ja v  a  2 s.c  o  m

    /**
     * Truncates blank spaces at the end of the string provided
     * @param str
     * @return String
     */
    public static String truncateEndBlanks(String str) {
        while (str.charAt(str.length() - 1) == ' ')
            str = str.substring(0, str.length() - 1);
        return str;
    }
}