Monday, 13 July 2015

How to Remove Duplicate White Spaces in String Using Java?

What is White space character in Java?

White space is a character which represents space between the words. There are two types of white spaces, those are Horizontal white space, Vertical white space.

Horizontal white spaces:

These are the white spaces entered through spacebar, Tab keys of the keyboard.

Vertical white spaces:

These are the white spaces entered through ? Enter key of the keyboard, which results a new line character. Older keyboards such as typewrite keyboards have Return, key meaning "Carriage-Return" ,which is equivalent to new line character.

So a white space character in a String can be a space, tab, new line, carriage return, form feed or vertical tab.

replaceAll() :

Use replaceAll() method of String class to remove white spaces. It's syntax is
public String replaceAll(String regex, String replacement)

It replaces the portion of the string that matched the given regular expression with the given replacement. To identify the multiple white spaces the regular expression in java is "\\s+". Which matches with one or more whitespace characters.

replacement could be any string of your choice. So to replace multiple spaces with single space, use " " (space) as replacement. Let's see  a sample code.
String testStr = "abc   cde"; // 2 space characters in between abc and cde

testStr = testStr.replaceAll("//s+"," "); // returns abc cde;

The replaceAll() method returns new string, because Strings are immutable in java, so keep in mind to assign the result to another String variable.

Complete Java Code:
package com.speakingcs.strings;

public class ReplaceWS {

static String testStr = "abc  cde";

public static void main(String[] args) {
    replaceWhiteSpaces(testStr);
}

 private static void replaceWhiteSpaces(String aStr) {
 
     String tempStr = null;

    // replacing with a single space
    tempStr = aStr.replaceAll("\\s+", " ");
    System.out.println("replaced with a single space");
    System.out.println(tempStr);

    // replacing with another word
    tempStr = aStr.replaceAll("\\s+"," ghi ");
    System.out.println("replaced with another word");
    System.out.println(tempStr);

    // replacing with new line character
    tempStr = aStr.replaceAll("\\s+", "\n");
    System.out.println("replaced with new line character");
    System.out.println(tempStr);

    // replacing with a tab character
    tempStr = aStr.replaceAll("\\s+", "\t");
    System.out.println("replaced with a tab character");
    System.out.println(tempStr);
}

The Output is:




0 comments:

Post a Comment

Note: only a member of this blog may post a comment.