Ruby: Split string at character, counting from the right side

后端 未结 6 1432
小蘑菇
小蘑菇 2020-12-10 00:59

Short version -- How do I do Python rsplit() in ruby?

Longer version -- If I want to split a string into two parts (name, suffix) at the first \'.\' character, this

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 01:42

    Here's what I'd actually do:

    /(.*)\.(.*)/.match("what.to.do")[1..2]
    => ["what.to", "do"]
    

    or perhaps more conventionally,

    s = "what.to.do"
    
    s.match(/(.*)\.(.*)/)[1..2]
    => ["what.to", "do"]
    

提交回复
热议问题