How to remove spaces from text?

When manipulating a string in Java or Python, you often need to remove the whitespaces from the text. The internet is awash with tools and methods that claim to remove whitespace from any string of data conveniently.

But it might take you considerable time shortlisting the tool that has high accuracy and friendly interface. Given this, we have handpicked the three most suitable methods to help you remove spaces from your text content.

Read it till the end and start getting rid of unnecessary whitespaces right away. Let’s find out!

Part 1. PDNob Windows Shortcuts

If you’re searching for the world’s simplest and most efficient text space remover, then get PDNob Windows Shortcuts. It is a professional tool that aims to provide a one-click solution to remove spaces from your text.

Unlike its competitors, PDNob Shortcuts is extremely easy to setup and use. All you need is to paste the text on its interface and hit the ‘remove spaces’ button. On top of it, you can customize the panel as you want like setting up hotkeys to increase productivity for future tasks.

One of the biggest advantages of removing whitespaces with this program is high accuracy. You don’t need to repeat the same procedure again and again, as in case of other tools, to get rid of all the unwanted spaces. It lets you assess its efficiency and functionality as a Windows 10 management tool. All this makes it the #1 space remover tool for Windows.

Key Features

Here are some notable features of this remarkable program:

  • The app offers a quick and convenient way to remove spaces from text.
  • It has built-in tools to make shortcuts for anything on your Windows including apps, files, folders, URLs, and more.
  • It has many useful features like OCR, screenshot, QR code generator & identifier, auto shutdown scheduler, etc.

Check out how easy it is to use PDNob Shortcut for removing spaces:

  • After installation, open the program on your PC. Next. press the middle mouse button or “Ctrl” key to activate the panel.

  • Now, you need to copy all the text you want to remove spaces from.

  • Go to the program and hit the “Remove space and copy” icon, the text will be pasted with space being removed.

    remove whitespace

Copy paste this text anywhere you wish. No other tool offers such a convenient way to remove whitespaces from a string or text content.

Part 2. Remove Whitespace Python

Next on our list to remove whitespace is using Python. There are multiple approaches to remove spaces from a string in Python. In each method, you need to enter a Python program to get rid of the spaces. Here, we will go with the replace() way.

With this method, you can remove all the whitespaces from string as well as from between words. Check out the below steps to proceed:

  • First, declare the string variable.

    s = ' Hello World From DigitalOcean \t\n\r\tHi There '

  • Next, apply the replace() method to remove duplicate spaces.

    s.replace(" ", "")

  • And you get the below output:

    'HelloWorldFromDigitalOcean\t\n\r\tHiThere'

Part 3. Remove Whitespace Java

Just like Python, Java also has built-in methods to remove whitespace regex from a string. Remember that Java lets you remove “all whitespace characters from the given string” as well as replace “consecutive whitespace characters with a single space and removing all leading and trailing whitespace characters.”

In the below guide, we will discuss one method for each case. Without any further ado, let’s cut to the chase:

Case 1: Removing All Whitespace from a String Using the ReplaceAll() Function

The replaceAll() function works perfectly with all regular expressions – also called regex. This method basically takes the regular expression as input to identify and replace the targeted substring.

You can remove any combination of spaces with the replaceAll() function. All you need to ensure is to create correct regular expression with correct replacement parameter.

Text Guide

Let’s see how this function works in the below case:

String result = myString.replaceAll("\\s", "");

assertThat(result).isEqualTo("IamawonderfulString!");

Run the above test and the output will show a string without all whitespaces.

Case 2: Replacing Consecutive Whitespace Characters with One Single Space Using String.replaceAll()

This is a two-fold method to remove whitespaces from a string. First, you need to replace consecutive whitespace with one single space. For this step, you can use the replaceAll() function with a regex to match consecutive whitespace characters and set one space as the replacement.

And the second step includes trimming the result of the first step using the String.trim() function to complete the task. Note that you can perform these two steps in any order you like.

Text Guide

Implement the below test case to check if this solution works for you:

String result = myString.replaceAll("\\s+", " ");

assertThat(result).isEqualTo(" I am a wonderful String ! ");

assertThat(result.trim()).isEqualTo("I am a wonderful String !");

Run it and it will replace consecutive whitespace characters with one single space. Hopefully, you won’t face any issue in how to remove whitespace regex from a string.

Final Remarks

And that brings us to the end of this article. We have learned how to remove whitespace from text or a string using three different methods. You can rely on Java or Python to get rid of the unwanted white spaces from a string.

Both these methods are easy to implement as you just have to write a “Python or Java function” to get the job done. However, if you want a universal tool that can remove any text without any hassles, then use PDNob Windows Shortcuts.

This powerful program is unbelievably easy to navigate even for a non-tech savvy user. Also, it has tons of amazing features that you can explore with its premium package.

PDNob Windows Shortcuts

Discover how PDNob can improve your productivity.