Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing '/' from rails string

So i have this command at the moment, ITs breaking up a URL to get the ID from said URL

The URL has a forward slash on the end. Which is currently bringing in the ID like this

1111111/

I need to remove the forward slash on the end so its like 1111111

Heres what i have at the moment

  var apis = '<%= @event.slink.split('-')[-1] %>';

I have currently already tried putting .gsub on the end however that didnt work (I may have not done this right however)

Thanks

like image 875
Sam Roberts Avatar asked Dec 08 '25 10:12

Sam Roberts


2 Answers

Try:

var apis = '<%= @event.slink.split('-')[-1].gsub(/\/$/, '') %>';
like image 147
Inpego Avatar answered Dec 10 '25 00:12

Inpego


For your simple case use chop, which returns a new String with the last character removed.

"1111111/".chop #=> "1111111"

If you need extracts URIs from a string and to remove more end chars:

require 'uri'

CHARS = %{.,'?/!:;}
URI.extract(text, ['http']).collect { |u| CHARS.index(u[-1]) ? u.chop : u } 

Demo: enter image description here

like image 41
Marko Tunjic Avatar answered Dec 09 '25 23:12

Marko Tunjic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!