The trim() function removes both the trailing and leading space, however, if I only want to remove the trailing space of a string, how can I do it?
As of JDK11 you can use stripTrailing:
JDK11
String result = str.stripTrailing();