Truncate string with Rails?

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

I want to truncate a string as follows:

input:

string = \"abcd asfsa sadfsaf safsdaf aaaaaaaaaa aaaaaaaaaa ffffdffffdffffdffffddd\"
7条回答
  •  有刺的猬
    2021-01-31 08:04

    This may not be the exact solution to your problem, but I think it will help put you in the right direction, with a pretty clean way of doing things.

    If you want "Hello, World!" to be limited to the first five letters, you can do:

    str = "Hello, World!"
    str[0...5] # => "Hello"
    

    If you want an ellipses, just interpolate it:

    "#{str[0...5]}..." #=> "Hello..."
    

提交回复
热议问题