Topic: How to remove leading   from string

Hi
  I Need small help that how to remove leading   tags
  My text is:
     str = "<p>Welcome to ruby &nbsp;&nbsp;</p> <p>&nbsp;&nbsp;</p>"
   I want result is
     str = "<p>Welcome to ruby</p>"

Can anybody help

Re: How to remove leading &nbsp; from string

str.gsub!('&nbsp;','')

Last edited by BradHodges (2010-08-02 11:45:50)

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to remove leading &nbsp; from string

Hi ,
Let me explain my requiremnt clearly.
Am usinng fck editor in rubyonrails.
So I can enter data is multiple paragraphs or single paragraph. but
after the  last paragraph if there is any spaces , i want to remove them
.

SO i entered data is like.
---------------------------------------------------------------
<p> pargraph1 pargraph1 &nbsp;&nbsp; </p>
<p>pargraph2 pargraph2 pargraph2 &nbsp;&nbsp; </p>
<p> pargraph3 hello3 hell13 &nbsp;&nbsp; </p>
<p> pargraph4 hello3 hell14 &nbsp;&nbsp; </p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;</p>
--------------------------------------------------------

In the above text 4th paragraph is the last paragrph which i
entered.after that i was pressed enter button so editor converted this
into " <p>&nbsp;&nbsp;&nbsp;&nbsp;</p"

I want to remove nbsp's after text in last paragrpah means result look
like
-------------------------------------------
<p> pargraph1 pargraph1 &nbsp;&nbsp; </p>
<p>pargraph2 pargraph2 pargraph2 &nbsp;&nbsp; </p>
<p> pargraph3 hello3 hell13 &nbsp;&nbsp; </p>
<p> pargraph4 hello3 hell14</p>
----------------------------------------------------------------

Re: How to remove leading &nbsp; from string

I'd have to see more code to answer specifically,  and I've never used fck,  but in general,  if you have a string of any kind,  and it has the non break space character in it,  you can use gsub to remove it, i.e.:

ugly = '<p>&nbsp;&nbsp;</p>'
pretty = ugly.gsub('&nbsp;','')

pretty now contains '<p></p>'

or
ugly.gsub!('&nbsp;','')

ugly now contains '<p></p>'
Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: How to remove leading &nbsp; from string

I assume you mean 'trailing' rather than 'leading' in the title. The following works on both your samples. You might like to test it against other scenarios...

str.gsub(/(?:(?:\s*&nbsp;)+(?:\s*<\/?p>){1,2})+$/,'</p>')