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));
    }//  w  w  w .  j  a  va  2s .  co 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;
    }
}