Java - Write code to Return the size of substring that does not contain any trailing spaces

Requirements

Write code to Return the size of substring that does not contain any trailing spaces

Demo

//package com.book2s;

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

    /**
     * Returns the size of substring that does not contain any trailing spaces
     * @param s the string
     * @return trimmed size
     */
    public static int rightTrimSize(String s) {

        int i = s.length();

        while (i > 0) {
            i--;

            if (s.charAt(i) != ' ') {
                return i + 1;
            }
        }

        return 0;
    }
}