Gossamer Forum
Home : General : Perl Programming :

String manipulation

Quote Reply
String manipulation
How can one convert commas in a string to spaces? And then convert all multiple spaces (in juxtaposition) to a single space?

For example:

Change:
-------------------------------
apple orange, banana kiwi,pear
-------------------------------

To:
-------------------------------
apple orange banana kiwi pear
-------------------------------

Thanks... Dan Smile
Quote Reply
Re: String manipulation In reply to
Shazam Batman, that's it! Thanks!!!!

Dan Smile
Quote Reply
Re: String manipulation In reply to
To convert commas into spaces:

$blah =~ s/,/ /g;

To convert multiple spaces into a single space:

$blah =~ s/\s+/ /g;

Basically, this equates to saying: replace anything that has at least 1 or more spaces to one space throughout the entire string ("g" == global).

Hope this helps,


------------------
Fred Hirsch
Web Consultant & Programmer