Is there a method for deleting a string identified in a variable from another string, e.g.
<rubycode>
myunwantedword = "Hello"
myphrase = "Hello world"
output = myphrase.suchamethod(myunwantedword)
</rubycode>
output is now " world"
Regards
_John Sampson_
Ricky_Ng
(Ricky Ng)
2
a = "hello world"
b = "hello "
a.sub!(b, "")
Is what I usually end up using. Hopefully I get corrected with a cleaner
way.
···
On Thu, Dec 20, 2012 at 10:11 AM, John Sampson <jrs.idx@ntlworld.com> wrote:
Is there a method for deleting a string identified in a variable from
another string, e.g.
<rubycode>
myunwantedword = "Hello"
myphrase = "Hello world"
output = myphrase.suchamethod(**myunwantedword)
</rubycode>
output is now " world"
Regards
_John Sampson_
--
Incoherently,
Ricky Ng
Hello,
yes, there is (two, actually). They are called String#gsub and String#gsub!, respectively.
Regards
7stud2
(7stud --)
4
Another way, if you only want to remove the first instance:
myunwantedword = "Hello"
=> "Hello"
myphrase = "Hello world"
=> "Hello world"
output = myphrase.dup
=> "Hello world"
output[myunwantedword] = ""
=> ""
output
=> " world"
Or you can use slice! instead of =
output.slice!(myunwantedword)
=> "Hello"
output
=> " world"
···
--
Posted via http://www.ruby-forum.com/\.
Many thanks - I did not know one could have a variable as an argument for sub or sub!
Regards
_John S_
···
On 20/12/2012 18:22, Calvin Bornhofen wrote:
Hello,
yes, there is (two, actually). They are called String#gsub and String#gsub!, respectively.
Regards
7stud2
(7stud --)
6
John Sampson wrote in post #1089759:
···
On 20/12/2012 18:22, Calvin Bornhofen wrote:
Hello,
yes, there is (two, actually). They are called String#gsub and
String#gsub!, respectively.
Regards
Many thanks - I did not know one could have a variable as an argument
Any place a literal, e.g. 10, 'hello', [1, 2, 3], is used, a variable
containing one of those values can be used in its place.
--
Posted via http://www.ruby-forum.com/\.