从字符串中删除子字符串 [英] Remove substring from the string
本文介绍了从字符串中删除子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想知道是否有任何方法可以从另一个字符串中删除字符串?像这样:
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
推荐答案
可以使用 slice 方法:
You can use the slice method:
a = "foobar"
a.slice! "foo"
=> "foo"
a
=> "bar"
有一个非 '!'版本也是如此.更多信息也可以在关于其他版本的文档中看到:http://www.ruby-doc.org/core/classes/String.html#method-i-slice-21
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
这篇关于从字符串中删除子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文