Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?
-Thanks
Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?
-Thanks
“monkey pants”.gsub(/ey/,’’)
=> ‘monk pants’
… I know the substring, its location, and length
Daniel S. wrote:
“monkey pants”.gsub(/ey/,’’)
=> ‘monk pants’
haha, thanks, i’ll give that a try
On 6/18/07, NB88 [email protected] wrote:
Quick question, is there a method for deleting substrings from within a
string. If not, does anyone have any suggestions?-Thanks
the gsub or gsub! methods will do this. They take a pattern (string or
regexp) and replace it with whatever you specify. In this case an empty
string.
http://www.ruby-doc.org/core/classes/String.html#M000839
e.g.
“The quck brown fox”.gub( /brown/, “” ) #=> “The quick fox”
Cheers
Daniel
Hi –
On Mon, 18 Jun 2007, Dan G. 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
One problem, the substring contains a ?, and is left in the resultant
string
unknown wrote:
Hi –
On Mon, 18 Jun 2007, Dan G. 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 stringDavid
thanks, that works
Dan G. 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.
Tim H. wrote:
Dan G. wrote:
One problem, the substring contains a ?, and is left in the resultant
stringIt 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=OY8AIhbqPHo
I’m trying to remove the watch? string; the slice method works, but it
would be better if you could use something like gsub
On 2007-06-17 22:58:18 -0500, Dan G. [email protected] said:
http://www.youtube.com/watch?v=OY8AIhbqPHo
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.com/watch?v=OY8AlhbqPHo”;
url.gsub(/?.$/,’’)
http://www.youtube.com/watch
=> nil
irb(main):002:0> str=“a?b?c”; str.gsub(/?.$/,’’)
=> “a”
On 18.06.2007 03:10, Dan G. wrote:
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
You could try :
url=‘http://www.youtube.com/watch?v=OY8AIhbqPHo’
url[/watch?/]=""
or with a postive lookahead : url[/watch?(?=v=OY8AIhbqPHo)/]=""
come wrote:
You could try :
url=‘http://www.youtube.com/watch?v=OY8AIhbqPHo’
url[/watch?/]=""or with a postive lookahead : url[/watch?(?=v=OY8AIhbqPHo)/]=""
Thanks…that last expression worked well but I have one last question:
I didn’t realize this before but I also need to remove the equal sign
after the v and replace it with a forward slash (’/’) so that it looks
like ‘http://www.youtube.com/v/OY8AIhbqPHo’
On Jun 18, 2007, at 4:03 AM, Dan G. wrote:
I didn’t realize this before but I also need to remove the equal sign
after the v and replace it with a forward slash (’/’) so that it looks
like ‘http://www.youtube.com/v/OY8AIhbqPHo’
so just do that:
url.sub(/watch?v=/, ‘v/’)
or if the variable wasn’t always ‘v’:
url.sub(/watch?(.)=/, ‘\1/’)
-Rob
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs