SPUG: multiple spaces in string

jerry gay jerry.gay at gmail.com
Wed Feb 8 11:31:10 PST 2006

On 2/8/06, luis medrano <lmzaldivar at gmail.com> wrote:
> List,
> I have to remove multiple spaces between a string. for example:
> "car   crash       in the freeway"
> where you can see is no uniformaty on how the spaces are showing but
> my questions is, how can remove the multiple spaces?
using the transliteration operator
  $string =~ tr/ //s;

may be more accurate than a regular expression
  $string =~ s/\s+//g;

as it won't replace tabs, or combinations of spaces and tabs.
generally, it's also faster, as it does not perform backtracking.

for more info on tr///, see 'perldoc perlop' and search for 'Transliterate'

More information about the spug-list mailing list