remove spaces at beginning and end of string java

Java
String withSpaces = "  Hi  ";
String withoutSpaces = withSpaces.trim();
Source

Also in Java: