Java - Write code to trim a string from Right by specified character

Requirements

Write code to trim a string from Right by specified character

Demo

//package com.book2s;

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

    public static String trimRight(String s) {
        return trimRight(s, ' ');
    }

    public static String trimRight(String s, Character c) {
        final int length = s.length();
        int i = length;
        while (i > 0 && s.charAt(i - 1) <= c) {
            --i;
        }
        return s.substring(0, i);
    }
}