Thursday, December 13, 2012

How to remove all white space from String in Java - beginning end and between words

String API in Java provides several utility method to remove white space from String in Java, from beginning, end and between words. White space means blank space including tab characters. One of the classic method to remove white space from beginning and end of a string in Java is trim() method, which removes white space from beginning and end. While using trim() method, key thing to remember is that trim() returns a different String object than 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 beginning, end and between words then you can use replaceAll() method of String and pass regular expression \s to find and replace all white spaces including tab with empty string "". This is the quickest ways of removing all white space from String in Java. Removing white spaces is not only common programming requirement in Java development but also one of the common String Interview question in Java. Once again just remember that every time you modify a String, even if you remove white space it generate a new String and remember to store and use that. In this Java tutorial we will see Java program to remove all white space from String in Java including beginning and end.

Java program to remove all white space form String in Java

How to delete white space from String in Java programHere is Java program which uses standard Java library to remove all white spaces from any String in Java. This program gives example of trim() method to delete white space from beginning as well ad from end. For removing all white space we have used regular expression along with replaceAll() method of String in Java. Regular expression to find white space is \s, which finds all white spaces including tab character.

package 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 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 beginning, end and middle of String. Just to note you can not remove white spaces only from middle of String or between words, because when you use replaceAll("\\s","") it also removes white spaces from beginning and end of String.

Other Java String tutorials you may like

3 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

Java67 Headline Animator