Truncate string with Rails?

前端 未结 7 627
醉酒成梦
醉酒成梦 2021-01-31 07:40

I want to truncate a string as follows:

input:

string = \"abcd asfsa sadfsaf safsdaf aaaaaaaaaa aaaaaaaaaa ffffdffffdffffdffffddd\"
7条回答
  •  梦毁少年i
    2021-01-31 08:08

    You can do almost the same without Rails:

    text.gsub(/^(.{50,}?).*$/m,'\1...')
    

    50 is the length you need.

提交回复
热议问题