Sonal S.
Sonal S.

Reputation: 1442

How can i get double quoted characters from a string in ruby on rails

If I have a string in a file:

str = hi "Sonal"

I am able to fetch this line of file in a string. Now I want to fetch the characters between the double quotes. i.e. Sonal. How can I do it in ruby?

Upvotes: 2

Views: 160

Answers (3)

maximus ツ
maximus ツ

Reputation: 8065

You can use regular expression like this,

given_string[/\".*\"/]

This will match the characters under quotes.

Upvotes: 1

oleg.vodopyan
oleg.vodopyan

Reputation: 186

or without regexp try something like this s[s.index('"')..s.rindex('"')]

Upvotes: 0

jvnill
jvnill

Reputation: 29599

try the following

'hi "Sonai"'.match(/"(?<inside_quote>.+)"/)[:inside_quote]

Upvotes: 4

Related Questions