Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring after the first = symbol in Ruby

Tags:

string

ruby

Purely out of curiosity, is there a more elegant way to simply get the substring after the first = symbol in a string? The following works to give back name=bob:

string = "option=name=bob"
string[string.index('=')+1..-1]

It just doesn't feel very Ruby. This also works:

string.split('=', 2)[1]

Again, not very elegant especially since split is doing extra unnecessary work. Are regular expressions the answer? I felt this was a little overkill for the simplicity of finding a single character position in a string:

string.match('=(.*)')[1]

I have to imagine this is an extremely common situation, isn't there a string.after('=') type method? Does Ruby on Rails provide something like this given the frequency this kind of operation is used over the web?

UPDATE: Forgot to mention the situation when the symbol is not found, nil or empty string should be returned. But the regular expression mechanism and .index method require an extra check for that (so less elegant).

like image 949
at. Avatar asked Dec 28 '12 18:12

at.


People also ask

How do you get the first element of a string in Ruby?

In Ruby, we can use the built-in chr method to access the first character of a string. Similarly, we can also use the subscript syntax [0] to get the first character of a string. The above syntax extracts the character from the index position 0 .

How do you extract a substring from a string in Ruby?

There is no substring method in Ruby, and hence we rely upon ranges and expressions. If we want to use the range, we have to use periods between the starting and ending index of the substring to get a new substring from the main string.

How do you remove the first character of a string in Ruby?

Removing the first character To remove the first character of a string in Ruby, we can use the built-in slice!() method by passing a character index.


5 Answers

Not exactly .after, but quite close to:

string.partition('=').last 

http://www.ruby-doc.org/core-1.9.3/String.html#method-i-partition

like image 110
dimuch Avatar answered Sep 26 '22 04:09

dimuch


There's also this way:

string.partition('=')[2] 

And this one:

string.sub(/.*?=/, '') 

I think I prefer the regexp way you mentioned, though.

like image 34
Thomas Avatar answered Sep 26 '22 04:09

Thomas


Probably not the Ruby-way (it's a bit cryptic), but you could do this:

string[/=/]
$'
=> "name=bob"

or

/=/ =~ string
$'
=> "name=bob"

$' is a global holding the string after a successful match. It's nil if nothing is matched, too!

like image 34
Brian Ustas Avatar answered Sep 24 '22 04:09

Brian Ustas


Use String#match

You can use a regular expression with positive lookbehind to find your match. For example:

string = "option=name=bob"
string.match /(?<==).*/
# => #<MatchData "name=bob">

Use Match Variables to Access Result

Even if you haven't assigned the match data to a variable, Ruby will store it in special match variables for you.

$&
# => "name=bob"
like image 32
Todd A. Jacobs Avatar answered Sep 25 '22 04:09

Todd A. Jacobs


split(char) is another function which can be used. For instance, we want to get substring before char ':' from "answer:computer" then, we can use "answer:computer".split(':')[0].So, we would get result as "answer".

like image 31
Gaurav Sachdeva Avatar answered Sep 25 '22 04:09

Gaurav Sachdeva