public static String rightTrim(String str) { String regex = "(.*\S+)(\s+$)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(str); if (m.matches()) { str = m.group(1); } return str; }