How to Remove All white spaces from String in Java? From Start, End and Between Words Examples

String API in Java provides several utility methods to remove white space from String in Java, from the beginning, end, and between words. White space means blank space including tab characters. One of the classic methods to remove white space from the beginning and end of a string in Java is the trim() method, which removes white space from beginning and end. While using the trim() method, the key thing to remember is that trim() returns a different String object than the older one because String is immutable in Java

On the other hand, if you want to remove all white spaces from string i.e. white space from the beginning, end, and between words then you can use the replaceAll() method of String and pass regular expression \s to find and replace all white spaces including the tab with empty string "".

This is the quickest way of removing all white space from String in Java. Removing white spaces is not only a common programming requirement in Java development but also one of the common String Interview questions in Java

Once again just remember that every time you modify a String, even if you remove white space it generates a new String, and remembers to store and use that. In this Java tutorial, we will see a Java program to remove all white space from String in Java including beginning and end.



Program to Remove all white space from String in Java - Example

Here is a Java program that uses a standard Java library to remove all white spaces from any String in Java. This program gives an example of the trim() method to delete white space from the beginning as well ad from the end. 

For removing all white space we have used regular expression along with the replaceAll() method of String in Java. Regular expression to find white space is \s, which finds all-white spaces including tab character. 

You can further see these Java Programming courses to learn more bout essential Java features like String and Regular Expressions. 

How to remove all white space from String in Java? Example


And, here is our complete Java program to remove all white space from given String in Java using trim and replaceAll methods. 


How to delete white space from String in Java programpackage test;

/**
 * Java program to remove while space in String. In this program, we will
 * see techniques to remove white space not only from beginning and end by using
 * trim() method of String class but also remove white space between String in Java.
 * e.g.  " ABC DEF " will turn to "ABCDEF". replaceAll() accepts regular expression
 * and \s can be used to remove all white space from String including space between
 * words.
 *
 * @author http://java67.blogspot.com
 */

public class StringRemoveWhiteSpace {
 
 
    public static void main(String args[]) {
     
        //removing white space from String from beginning and end in Java      
        String strWhiteSpace = "    This String contains White Space at beginning and end and middle    ";
        System.out.printf("String before removing White space : %n %s %n", strWhiteSpace);
        System.out.printf("length of String before removing white space %d : ", strWhiteSpace.length());
     
        //trim() method can remove white space from the beginning and end of String in Java
        String strWithoutWhiteSpace = strWhiteSpace.trim();
        System.out.printf("String after removing white space from beginning and end %n %s %n", strWithoutWhiteSpace);
        System.out.printf("length of String after removing white space from beginning and end %d : ", strWithoutWhiteSpace.length());

 
     
        //removing white space between String in Java
        String white space = "ABC DEF GHI";
        System.out.println("String with white space between words: " + white space);
     
        // \s is regular expression for white space tab etc
        String withoutspace = whitespace.replaceAll("\\s", "");
        System.out.println("String after removing white space between words and everywhere: " + withoutspace);      
     
    }    
   
}

Output:
String before removing White space :
     This String contains White Space at beginning and end and middle  
length of String before removing white space 72: String after removing white space from beginning and end
 This String contains White Space at beginning and end and middle
length of String after removing white space from beginning and end 64 : String with white space between words: ABC DEF GHI

String after removing white space between words and everywhere: ABCDEFGHI


That's all on How to remove all white space from String in Java, including white space from the beginning, end, and middle of String. Just to note you can not remove white spaces only from the middle of String or between words, because when you use replaceAll("\\s","") it also removes white spaces from the beginning and end of String.


Other Java String tutorials you may like





4 comments:

  1. Doesn't using trim() method generates lots of garbage intermediary String object, which can full permGen space of heap and result in either excessive Garbage Collection and eventually crashing program with java.lang.OutOfMemoryError?

    ReplyDelete
  2. thank you it works well with me ...

    ReplyDelete
  3. You have to use [\s\u00a0] instead of simple \s if you need to remove unbreakable spaces...

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.