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

后端 未结 6 1433
小蘑菇
小蘑菇 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

    String#rpartition does just that:

    name, match, suffix = name.rpartition('.')
    

    It was introduced in Ruby 1.8.7, so if running an earlier version you can use require 'backports/1.8.7/string/rpartition' for that to work.

提交回复
热议问题