Get substring after the first = symbol in Ruby

后端 未结 5 1705
盖世英雄少女心
盖世英雄少女心 2021-02-02 05:48

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=b

5条回答
  •  长情又很酷
    2021-02-02 06:47

    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!

提交回复
热议问题