Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Replacing multiple spaces into one character?

by John M. Dlugosz (Monsignor)
on Oct 09, 2001 at 07:10 UTC ( [id://117626] : note . print w/replies, xml ) Need Help??


in reply to Replacing multiple spaces into one character?

Er, it works for me. To wit:
$str= " perlmonks is the coolest"; $str =~ s/\s+/_/g; print $str;
prints: _perlmonks_is_the_coolest

(I left out the i modifier because space is the same in capital or lower-case. I don't think that would change the behavior, though!)

—John

Replies are listed 'Best First'.
Re: Re: Replacing multiple spaces into one character?
by Boldra (Deacon) on Oct 09, 2001 at 18:26 UTC
    Agreed, it works exactly as r.joseph presented it. However you may want to think about using
    $str =~ s/ +/_/g;
    instead, since it doesn't mess with your newline characters. That is, with your version "perl\n" becomes "perl_".

    - Boldra