Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Sunday, November 7, 2021

Java Strings - Removing White Spaces From A String

1. Overview

In this tutorial, we'll learn how to remove all white spaces from java strings. This is common interview programming questions asked for junior level java developers.

Like input string can contain white space or tab spaces. All of these should be removed at the final output.

Removing whitespaces from string can be done using String api methods and third party apis methods as well.

Sample input and outputs:

i/p 1: "hello welcome to the new place \n how are you feeling?"
o/p 1: "hellowelcometothenewplacehowareyoufeeling?"

i/p 2: " this is tab based string "
o/p 2: "thisistabbasedstring"

Let us explore all the ways with example programs.

Java Strings - Removing White Spaces From A String


2. Java - Remove Whitespace from String Using String API


Java String class comes with a replaceAll() method which takes input a regular expression and replacement value. Whatever the portion or substring that matched with the given regex will be replaced with the given replacement character set.

In this case, regular expression will be "\\s" and replacement String will be "".

"\s" removes all white spaces including non visible characters such as '\t', '\n' or '\r'.

Look at the below example program how replaceAll() method works.
package com.javaprogramto.programs.strings.whitespaces;

public class StringRemoveWhiteSpacesExample {

	public static void main(String[] args) {
		String regex = "\\s";
		String replacementString = "";

		// example 1
		String string1 = "hello welcome to the new place \n how are you feeling?";

		String output1 = string1.replaceAll(regex, replacementString);

		System.out.println("Input str 1 : " + string1);
		System.out.println("Output str 1 : " + output1);

		// example 2
		String string2 = "	this is tab 	based string 	";

		String output2 = string2.replaceAll(regex, replacementString);

		System.out.println("Input str 2 : " + string2);
		System.out.println("Output str 2 : " + output2);

	}
}

Output:
Input str 1 : hello welcome to the new place 
 how are you feeling?
Output str 1 : hellowelcometothenewplacehowareyoufeeling?
Input str 2 : 	this is tab 	based string 	
Output str 2 : thisistabbasedstring

3. Java - Remove Whitespace from String Using Apache Commons


Apache commons lang api has support for the deletion of empty or whitespace in any form such as \n, \t or \r forms.

Use StringUtils.deleteWhitespaces(String) method to remove all whitespaces for the given string.

Look at the below example.
package com.javaprogramto.programs.strings.whitespaces;

import org.apache.commons.lang3.StringUtils;

public class StringRemoveWhiteSpacesExample2 {

	public static void main(String[] args) {

		System.out.println("Using apache commons lang api");

		// example 1
		String string1 = "hello madam";

		String output1 = StringUtils.deleteWhitespace(string1);

		System.out.println("\nInput str 1 : " + string1);
		System.out.println("Output str 1 " + output1);

		// example 2
		String string2 = "	hello madam, welcome 	to the new house";

		String output2 = StringUtils.deleteWhitespace(string2);

		System.out.println("\nInput str 2 : " + string2);
		System.out.println("Output str 2 " + output2);

	}

}

Output:
Using apache commons lang api

Input str 1 : hello madam
Output str 1 hellomadam

Input str 2 : 	hello madam, welcome 	to the new house
Output str 2 hellomadam,welcometothenewhouse

4. Java - Remove Whitespace from String Using Spring Framework


Additionally, Spring framework is added with the method which deletes and trims the whitespaces.

Use trimAllWhitespaces(String) method to clear all whitespaces in the given string.

Look at the below example.
package com.javaprogramto.programs.strings.whitespaces;

import org.springframework.util.StringUtils;

public class StringRemoveWhiteSpacesExample3 {

	public static void main(String[] args) {

		System.out.println("Using spring framework api");

		// example 1 - trimAllWhitespace()
		String string1 = "hello madam";

		String output1 = StringUtils.trimAllWhitespace(string1);

		System.out.println("\nInput str 1 : " + string1);
		System.out.println("Output str 1 " + output1);

		// example 2 - trimAllWhitespace()
		String string2 = "	hello madam, welcome 	to the new house";

		String output2 = StringUtils.trimAllWhitespace(string2);

		System.out.println("\nInput str 2 : " + string2);
		System.out.println("Output str 2 " + output2);
	}
}

Output:
Using spring framework api

Input str 1 : hello madam
Output str 1 hellomadam

Input str 2 : 	hello madam, welcome 	to the new house
Output str 2 hellomadam,welcometothenewhouse


5. Conclusion


In this article, We've seen how to take out all white spaces from string in java.

Examples are shown with String, apache commons lang and spring framework api.





No comments:

Post a Comment

Please do not add any spam links in the comments section.