[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing strings

NB88

6/18/2007 12:43:00 AM

Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?

-Thanks

--
Posted via http://www.ruby-....

13 Answers

Daniel Sheppard

6/18/2007 12:47:00 AM

0

"monkey pants".gsub(/ey/,'')
=> 'monk pants'

> -----Original Message-----
> From: list-bounce@example.com
> [mailto:list-bounce@example.com] On Behalf Of NB88
> Sent: Monday, 18 June 2007 10:43 AM
> To: ruby-talk ML
> Subject: Parsing strings
>
> Quick question, is there a method for deleting substrings
> from within a
> string. If not, does anyone have any suggestions?
>
> -Thanks
>
> --
> Posted via http://www.ruby-....
>
>

NB88

6/18/2007 12:48:00 AM

0

... I know the substring, its location, and length

--
Posted via http://www.ruby-....

NB88

6/18/2007 12:49:00 AM

0

Daniel Sheppard wrote:
> "monkey pants".gsub(/ey/,'')
> => 'monk pants'

haha, thanks, i'll give that a try

--
Posted via http://www.ruby-....

NB88

6/18/2007 12:58:00 AM

0

One problem, the substring contains a ?, and is left in the resultant
string

--
Posted via http://www.ruby-....

dblack

6/18/2007 1:00:00 AM

0

NB88

6/18/2007 1:11:00 AM

0

unknown wrote:
> Hi --
>
> On Mon, 18 Jun 2007, Dan German wrote:
>
>> ... I know the substring, its location, and length
>
> You can use slice!.
>
> string = "This is a string"
> string.slice!(4,5)
> puts string # => This string
>
>
> David

thanks, that works

--
Posted via http://www.ruby-....

Tim Hunter

6/18/2007 1:16:00 AM

0

Dan German wrote:
> One problem, the substring contains a ?, and is left in the resultant
> string
>
>
It would help if you'd show an actual string you have and what you'd
like it to become. If you have two examples, even better.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


NB88

6/18/2007 3:58:00 AM

0

Tim Hunter wrote:
> Dan German wrote:
>> One problem, the substring contains a ?, and is left in the resultant
>> string
>>
>>
> It would help if you'd show an actual string you have and what you'd
> like it to become. If you have two examples, even better.

It's actually a youtube url: ex.
http://www.youtube.com/watch?v=O...
I'm trying to remove the watch? string; the slice method works, but it
would be better if you could use something like gsub

--
Posted via http://www.ruby-....

BDB

6/18/2007 4:06:00 AM

0

On 2007-06-17 22:58:18 -0500, Dan German <germans88@yahoo.com> said:

> Tim Hunter wrote:
>> Dan German wrote:
>>> One problem, the substring contains a ?, and is left in the resultant
>>> string
>>>
>>>
>> It would help if you'd show an actual string you have and what you'd
>> like it to become. If you have two examples, even better.
>
> It's actually a youtube url: ex.
> http://www.youtube...?v=O...
> I'm trying to remove the watch? string; the slice method works, but it
> would be better if you could use something like gsub

gsub(/\?.*$/,'') will delete the first question mark until the end of
the string.

irb(main):001:0> url = "http://www.youtube...?v=OY8Alhbq...
url.gsub(/\?.*$/,'')
http://www.youtube...
=> nil
irb(main):002:0> str="a?b?c"; str.gsub(/\?.*$/,'')
=> "a"

--
H. Asari

Robert Klemme

6/18/2007 6:46:00 AM

0

On 18.06.2007 03:10, Dan German wrote:
> unknown wrote:
>> Hi --
>>
>> On Mon, 18 Jun 2007, Dan German wrote:
>>
>>> ... I know the substring, its location, and length
>> You can use slice!.
>>
>> string = "This is a string"
>> string.slice!(4,5)
>> puts string # => This string
>>
>>
>> David
>
> thanks, that works

Alternative:

irb(main):001:0> string = "This is a string"
=> "This is a string"
irb(main):002:0> string[4,5]=''
=> ""
irb(main):003:0> string
=> "This string"

Kind regards

robert