remove space from end of string java code example
Example 1: remove last character from string java
private static String removeLastChar(String str) {
return str.substring(0, str.length() - 1);
}
Example 2: how to strip trailing spaces in java
String str = new String(" apples ");
str.trim(); // result is a nes string "apple"
Example 3: remove spaces at beginning and end of string java
String withSpaces = " Hi ";
String withoutSpaces = withSpaces.trim();