RajG
RajG

Reputation: 832

split the string to get specific value on ruby on rails?

I want to get two separate value from

/en/faq

the two separate value will be

lang =en
rem  =faq

I have used to split which was relatively much easier. Nonetheless, this is my approach which needs tweaking around, hopefully from your help I will be able to accomplish it.

string = "/en/faq"
lang   = string.split("/").first
rem    = string.split("/en/")

puts "/#{lang}/#{rem[1]}"

The desired output should be "/en/faq/" but the output is

"//faq" 

i know i have got '.first' which is why I am getting a null value but could anyone help on getting the right results please?

thanks in advance.

Upvotes: 0

Views: 585

Answers (1)

RajG
RajG

Reputation: 832

string = "/en/faq"
lang   = string.split("/")
rem    = string.split("/#{lang[1]}/")

puts "/#{lang[1]}/#{rem[1]}"

this does the trick and thanks to Sebi for his prompt answer!

Upvotes: 1

Related Questions