从字符串末尾重复一个字符

时间:2014-02-28 15:40:10

标签: ruby string

我想从字符串的末尾重复删除一个特定的字符,只留下一个唯一的字符。

所以我希望以下情况发生:

buns = "well, that was unusual....."
buns.dedupe(".")
#=> "well, that was unusual."

提前致谢!

1 个答案:

答案 0 :(得分:1)

如果您只想从字符串末尾删除重复字符(而不是字符串中的任何位置,如String#squeeze那样):

"well, that was unusual.....".gsub(/\.+$/, '.')