Thursday, May 7, 2015

Forcing Spaces to be Preserved

Some web-based text editors (especially e-mail clients and forums) have an unfortunate tendency to destroy extra spaces when text is pasted in. So something like this:

\  /-----\
 > |  !  |
/  \-----/

Would become smushed like this:

\ /-----\
> | ! |
/ \-----/

This is obviously a serious problem for hand-aligned tables and other monospace text, which as a programmer, I deal with on a semi-regular basis.

Fortunately, there is a (fairly ugly) workaround that forces extra spaces to be preserved... in a way. The 255th ASCII character typically renders as a blank space, but is of course not the same as character 32, the real space.

To convert spaces in some text to these fake spaces, open a Notepad window, paste your text (danger: may destroy carriage returns if the source uses only LF rather than CR-LF; use Word or Notepad++ if appropriate), open the Replace window, type a single space in the "find" field, insert character 255 in the "replace with field" by holding the right-hand Alt key while typing 255 on the numeric keypad with NumLock on, and choose to replace all. Paste the text into the space-eating text field, and you'll find it remains intact.

Careful: this will wreak havoc if pasted into a code file or if the file is textually transcoded.

No comments:

Post a Comment