Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Blank Space In String

Status
Not open for further replies.

nithink

Programmer
Nov 7, 2002
92
0
0
US
Hi,
Need to remove space, if found in a String.

Ex :

if $x=" " then $x="";

Your help appreciated.
Thanks.
 
new2everything,

Consider, if you will:

Code:
#!/usr/bin/perl

my $a = "  This string contains      white space.   ";

if ( $a =~ s/\s//g ) {
   print "$a\n";
}

This uses a regular expression to substitute nothing for any whitespace characters globally within the value of $a.

For a really good introduction, please see the perlretut man page (online at
Hope this helps...

-- Lance
 
Thanks Lance. Will try it and let yu know.
 
One small problemo with footpad's answer. If you run that code, your output should be:
Whattimedidyouhaveinmind?

try this instead:
if ( $a =~ s/\s{2,}/\s/g ) {

The {x,y} part puts a minimum and maximum number of hits. It says change 2 or more instances of a whitespace with only one whitespace.
In this case, $a will end up with a leading whitespace, but the link above shoul show you how to get rid of leading (and trailing) characters. Don't to ruin the surprise for you. :) (hint: check "anchors" in an Perl regex tutorial).
 
Oops -- copied the wrong buffer, but the gist is the same. The output would be:
Thisstringcontainswhitespace.
 
$text = " This string contains white space. ";

print "before: $text\n";

$text =~ s/^\s+//; # trim leading whitespace
$text =~ s/\s{2,}/ /g; # trim whitespace within text
$text =~ s/\s+$//; # trim trailing whitespace

print " after: $text\n";


Kind Regards
Duncan
 
Very true all...and just to hilight perl's great ability to solve the same problem in different ways...you can more simply go:


Code:
($text)=$text=~/^\s*(.*?)\s*$/;  # trim head/tail at same time
$text=~s/\s+/ /;   # faster than using the [2,] notation..

# note in the first line the .*? will match the whole line but the ? part says to be non-greedy meaning
# it will allow the \s* to suck up any spaces at the head/tail without a fight...
# the second line just says substitute all spaces/tabs to a single space (less arcane than the [2,] thing so
# other coders wont get slowed down when reading your stuff... clear is better than cute.



CGE
 
Just be careful that \s matches any whitespace, not just spaces. A newline or a tab gets turned into a space, too. And if you go with \s{2,} then you still have issues if you have trailing whitespace on a line before the newline (like wordwrap sometimes generates) or if you have leading whitespace on the following line (like indentation) Both get eaten to a single space.

Is + really faster than {2,} ? I know that high upper bounds are very slow, but both + and {2,} are greedy and attempt matching as far right as possible. I suppose there may be optimizations builtin for the more common +, especially for pattern compiles, but I don't know. Well, at the very least, + is a lot easier to type. :)

________________________________________
Andrew - Perl Monkey
 
yes, \s will also match tabs...I didnt think we were after blanklines too...well, we can just go:

$text=" sdf adsf ds s s sd sdf s df dafs ";
foreach $line (split(/\n/,$text)) {
($line)=$line=~/^\s*(.*?)\s*$/m;
$line=~s/\s+/ /g;
print "new line : is \"$line\"\n";
}


so it will split the input line up into lines each with a return on the end and do its thing...

as for the faster with the \s+ thing, I meant faster to type/read etc...(sorry for the confusing wording!!) as for machine speed I think it'll be a little faster to do the {2,} because it will avoid changing the string more often, the match will fail on single spaces and even though the match will take longer it will save time overall..
p.s. no one caught me on my not using the g on my substitute before! whew!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top