code: private static int findEndTrimWhitespace(CharSequence s)
     {
        for (int i = s.length(); i > 0; i--)
        {
           if (!Character.isWhitespace(s.charAt(i - 1)))
           {
              return i;
           }
        }
        return 0;
     }
docs: Finds and returns the end of the given character sequence after trimming
  white space characters from the right. Whitespace characters are defined
  by {@link Character#isWhitespace(char)}. .
  
  @param s
  the character sequence
  @return the index after the last non-whitespace character, or zero if all
  characters are blank