How do I remove a substring after a certain character in a string using Ruby?

前端 未结 5 1943
温柔的废话
温柔的废话 2020-12-29 02:05

How do I remove a substring after a certain character in a string using Ruby?

5条回答
  •  攒了一身酷
    2020-12-29 02:47

    I'm surprised nobody suggested to use 'gsub'

    irb> "truncate".gsub(/a.*/, 'a')
    => "trunca"
    

    The bang version of gsub can be used to modify the string.

提交回复
热议问题