ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to remove all white spaces between strings or from strings using Java?

Updated on November 22, 2013

Today, I was implementing a funny java work. The solution of the problem is really simple but at the beginning I stacked to solve. The problem was to remove all the white spaces among all the strings and replace those white space(s) with a single comma(,).

To do this, I firstly tried using replace() method. Replace() method may give you the correct solution like if the string is "Google Yahoo". And after using that function it returns "Google,Yahoo". Java code for doing this are the following:

public class RemoveAllSpaces {

	public static void main(String[] args) {

		String originalString = "Google Yahoo";
		String finalString;
	
//		finalString = originalString.replaceAll("\\s+", ",");
		finalString = originalString.replace(" ", ",");
				
		System.out.println("Original String (with spaces)...." + originalString);
		System.out.println("Final String (without spaces)...." + finalString);

	}

}

The out put of the code is like the following:

But if the string contains more then one white space then what will be the scenario? Suppose, the string is like "Google Yahoo Bing". Using the above concepts, lets see what happens...

public class RemoveAllSpaces {

	public static void main(String[] args) {

		String originalString = "Google Yahoo     Bing";
		String finalString;
	
		finalString = originalString.replace(" ", ",");
				
		System.out.println("Original String (with spaces)...." + originalString);
		System.out.println("Final String (without spaces)...." + finalString);

	}

}

The output of the above code will be like the following:

So, we are seeing that all white spaces are replaced by ,. Our desired output is "Google,Yahoo,Bing". But instead of getting that output we are getting the result "Google,Yahoo,,,,,Bing".


The solution of the problem may be like this also:

finalString = originalString.replace(" ", ",");   // for 1 space
finalString = originalString.replace("  ", ",");  // for 2 spaces
finalString = originalString.replace("   ", ","); // for 3 spaces
finalString = originalString.replace("    ", ",");// for 4 spaces

You can replace those blank white-space using the above snippets. But it's not efficient and may not always give you the correct result.

Solution:

Then, what is the solution? The simple and efficient solution is to use regular expression (i.e. regex). Just use regular expression and it solves all the things. The only line of code you may need like the following:

finalString = originalString.replaceAll("\\s+", ",");
public class RemoveAllSpaces {

	public static void main(String[] args) {

		String originalString = "Google Yahoo     Bing";
		String finalString;
	
		finalString = originalString.replaceAll("\\s+", ",");
				
		System.out.println("Original String (with spaces)...." + originalString);
		System.out.println("Final String (without spaces)...." + finalString);

	}

}

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article