从字符串中删除字符序列

时间:2022-02-06 00:06:18

In rails I simply want to remove 'http://' or 'https://' from a string if they are present. Currently I'm achieving this with the below code:

在rails中,我只想从字符串中删除‘http://’或‘https://’,如果它们存在的话。目前我通过以下代码来实现这个目标:

install_url.delete('http://').delete('https://')

I feel like this might not be the best way to do it. Are there any better suggestions?

我觉得这可能不是最好的方法。有更好的建议吗?

Thank you!

谢谢你!

2 个解决方案

#1


2  

You can use regular expressions:

你可以使用正则表达式:

install_url.sub(/^http[s]?:\/\//, '')

#2


1  

This should do the trick:

这应该可以做到:

install_url.gsub(/https?:\/\//, '')

#1


2  

You can use regular expressions:

你可以使用正则表达式:

install_url.sub(/^http[s]?:\/\//, '')

#2


1  

This should do the trick:

这应该可以做到:

install_url.gsub(/https?:\/\//, '')