Remove spaces in JAVA
1. String.trim()
trim() removes leading and trailing spaces
2.str.replace(" ", ""); Remove all spaces, including the beginning, the end, and the middle
Copy the code code as follows:
String str = " hell o ";
String str2 = str.replaceAll(" ", "");
System.out.println(str2);
3. Or replaceAll(" +",""); remove all spaces
4.str = .replaceAll("//s*", "");
Can replace most whitespace characters, not limited to spaces
/s can match any of the whitespace characters such as spaces, tabs, form feeds, etc.