8. Removing white spaces from a string

The solution to this problem consists of using the String.replaceAll() method with the s regular expression. Mainly, s removes all white spaces, including the non-visible ones, such as , , and  :

public static String removeWhitespaces(String str) {
return str.replaceAll("\s", "");
}
Starting with JDK 11, String.isBlank() checks whether the string is empty or contains only white space code points. For third-party library support, please consider Apache Commons LangStringUtils.deleteWhitespace(), and the Spring Framework, StringUtils.trimAllWhitespace().
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset