When Is a Space Not a Space?

Trying to replace spaces in your text, or split text by the spaces, but it won’t work? Did you get the text from a web page? Then the ‘space’ is probably not a ‘space’ — it is a ‘non-breaking space’. Here’s how to deal with it.

What Is a Non-Breaking Space?

In most text that you type on a keyboard, a space is a character identified by ASCII code 32, and is commonly used to insert a gap between words. People will sometimes insert multiple spaces to make the gaps bigger.

But in the web, they will often ignore spaces when presenting text, so 25 spaces will get shrunk to one. There are also times in digital content creation where you don’t want automatic line wrapping to break up a sequence like ‘100 km’. Content creators typically know this, as do many of the development tools they use, so they may automatically convert spaces in the text into a different character — ASCII code 160, which is referred to as a ‘non-breaking space‘. They look the same but are treated differently:

This line uses multiple ASCII code 32 spaces.
This line uses multiple ASCII code 160     non-breaking      spaces.

Copying Content From The Web

When you copy content from a web page, it could contain these non-breaking spaces, so if you download some data and try to work with it, you may find you can’t split by the space character, or can’t replace the space character with something else. It can be very frustrating!

This is usually because you are trying to split or replace ASCII code 32 characters (spaces), and the content you copied contains ASCII code 160 characters (non-breaking spaces).

[Note that there are other characters that also look like a space, but aren’t].

Simple Workaround

The easiest way to deal with this is to replace non-breaking spaces with spaces first. Each application has different ways to type a non-breaking space, which can be a pain to figure out. The easiest way is to simply copy one of the problematic spaces from your data, then do a search and replace to replace <paste the copied character> with a space that you type. Yes, it looks like you are doing nothing…but press on!

Now you have ‘clean’ text to work with. Try the split or replce operation — it will probably work now.