Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string which contains multiple forward slashes

Tags:

ruby

I have a string as given below,

./component/unit

and need to split to get result as component/unit which I will use this as key for inserting hash.

I tried with .split(/.\//).last but its giving result as unit only not getting component/unit.

like image 588
Karthi1234 Avatar asked Sep 05 '26 06:09

Karthi1234


1 Answers

I think, this should help you:

string = './component/unit'

string.split('./') 
#=> ["", "component/unit"]

string.split('./').last
#=> "component/unit"
like image 128
Alex Holubenko Avatar answered Sep 07 '26 22:09

Alex Holubenko



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!