I am just wondering if there is any method to remove string from another string? Something like this:
class String
def remove(s)
self[s.length, self.length - s.length]
end
end
You can use the slice method:
a = "foobar"
a.slice! "foo"
=> "foo"
a
=> "bar"
there is a non '!' version as well. More info can be seen in the documentation about other versions as well: http://www.ruby-doc.org/core/classes/String.html#method-i-slice-21
[]s for the non-bang version."foobar".tap{|s| s.slice!("foo")}.upcasedelete wouldn't work, since it deletes all the characters you pass in: 'hello world'.delete('hello') #=> ' wrd'How about str.gsub("subString", "")
Check out the Ruby Doc
sub would be more appropriate than gsub, since the OP only wants to remove the substring from the beginning of the string, not all through the string (look at his sample code). And using a regex, like this, would be better: str.sub(/^subString/, '') -- because it ensures that the substring will definitely be removed only from the beginning.subString doesn't include any regex special characters./^subString/ is a literal, so we can be very sure it doesn't include any metacharacters. If you are substituting some other string into a regex, you can do this: /#{Regexp.escape(str)}/.Regexp.escape(), though.If it is a the end of the string, you can also use chomp:
"hello".chomp("llo") #=> "he"
chomp in combination with reverse: "hello".reverse.chomp("he".reverse).reverse #=> "llo"If your substring is at the beginning of in the end of a string, then Ruby 2.5 has introduced the methods for this:
If you only have one occurrence of the target string you can use:
str[target] = ''
or
str.sub(target, '')
If you have multiple occurrences of target use:
str.gsub(target, '')
For instance:
asdf = 'foo bar'
asdf['bar'] = ''
asdf #=> "foo "
asdf = 'foo bar'
asdf.sub('bar', '') #=> "foo "
asdf = asdf + asdf #=> "foo barfoo bar"
asdf.gsub('bar', '') #=> "foo foo "
If you need to do in-place substitutions use the "!" versions of gsub! and sub!.
asdf['bar'] = ''If you are using Rails there's also remove.
E.g. "Testmessage".remove("message") yields "Test".
Warning: this method removes all occurrences
slice method doesn't return the portion of the string that's sliced, it returns the "knife"slice! def gimme_the_slice(my_string, my_slice) my_string.slice!(my_slice) my_string If you are using rails or at less activesupport you got String#remove and String#remove! method
def remove!(*patterns)
patterns.each do |pattern|
gsub! pattern, ""
end
self
end
source: http://api.rubyonrails.org/classes/String.html#method-i-remove
If I'm interpreting right, this question seems to ask for something like a minus (-) operation between strings, i.e. the opposite of the built-in plus (+) operation (concatenation).
Unlike the previous answers, I'm trying to define such an operation that must obey the property:
IF c = a + b THEN c - a = b AND c - b = a
We need only three built-in Ruby methods to achieve this:
'abracadabra'.partition('abra').values_at(0,2).join == 'cadabra'.
I won't explain how it works because it can be easily understood running one method at a time.
Here is a proof of concept code:
# minus_string.rb
class String
def -(str)
partition(str).values_at(0,2).join
end
end
# Add the following code and issue 'ruby minus_string.rb' in the console to test
require 'minitest/autorun'
class MinusString_Test < MiniTest::Test
A,B,C='abra','cadabra','abracadabra'
def test_C_eq_A_plus_B
assert C == A + B
end
def test_C_minus_A_eq_B
assert C - A == B
end
def test_C_minus_B_eq_A
assert C - B == A
end
end
One last word of advice if you're using a recent Ruby version (>= 2.0): use Refinements instead of monkey-patching String like in the previous example.
It is as easy as:
module MinusString
refine String do
def -(str)
partition(str).values_at(0,2).join
end
end
end
and add using MinusString before the blocks where you need it.
here's what I'd do
2.2.1 :015 > class String; def remove!(start_index, end_index) (end_index - start_index + 1).times{ self.slice! start_index }; self end; end;
2.2.1 :016 > "idliketodeleteHEREallthewaytoHEREplease".remove! 14, 32
=> "idliketodeleteplease"
2.2.1 :017 > ":)".remove! 1,1
=> ":"
2.2.1 :018 > "ohnoe!".remove! 2,4
=> "oh!"
Formatted on multiple lines:
class String
def remove!(start_index, end_index)
(end_index - start_index + 1).times{ self.slice! start_index }
self
end
end