I Spy Code - Java

Delete all whitespace from a string

Question:

Write a java program that delete all of the white spaces in a string.

Solution:

Here is a java example that removes all whitespaces from a string:

Source: (Example.java)

import org.apache.commons.lang.StringUtils;
 
public class Example {
 
    public static void main(String[] args) {
        String str1 = "the cat in the \t hat";
        System.out.println(str1);
 
        String str2 = StringUtils.deleteWhitespace(str1);
        System.out.println(str2);
    }
 
}
 

Output:

$ java Example
the cat in the 	 hat
thecatinthehat

References:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html#deleteWhitespace-java.lang.String-

Questions answered by this page:

How to remove all white spaces in java
Remove multiple spaces from string in java