String originalString = " This is a string with whitespaces. "; String processedString = StringUtils.deleteWhitespace(originalString); System.out.println(processedString); // "Thisisastringwithwhitespaces."
String[] stringsArray = {" This is a string with whitespaces. ", " Another string with many spaces. "}; for (String str : stringsArray) { String processedString = StringUtils.deleteWhitespace(str); System.out.println(processedString); } // "Thisisastringwithwhitespaces." // "Anotherstringwithmanyspaces."In order to use the StringUtils class, you need to include the Apache Commons Lang library in your project.