function TrimRight(const S: String): String;
Trims trailing spaces and control characters from the given string S.