If you using Java 11, you can use the String::strip method. For example:
int integer = Integer.parseInt(string.strip());
The strip() method is similar to trim() but uses Character.isWhitespace(int) to resolve spaces. (The Character.isWhitespace(int) method is Unicode-aware.)