Ruby-on-Rails:混合Sanitize和Truncate可能很脏

问题描述 投票:1回答:1

所以我独自一人得到了我需要的东西。但是我想截断它,我的动态文本出现了带有Microsoft Word垃圾的脏文本。

一个例子 :

≪! [If Gte Mso 9]>≪Xml>  ≪Br /> ≪O:Office Document Settings>  ≪Br /> ≪O:Allow Png/>  ≪Br /> ≪/O:Off...

那么我如何才能充分利用这两个世界呢?是否有速记红宝石的方式来做到这一点?例如,一个gsub语句会在第125个字符之后剪掉一切?

ruby-on-rails ruby sanitization truncate
1个回答
2
投票

如果你只想切片,你可以

>> long_ugly_string = "omg this is a long string"
=> "omg this is a long string"
>> long_ugly_string[10..-1]
=> "s a long string"

参考:http://ruby-doc.org/core/classes/String.html#M000771

所以,你只是指定起始字符(10)和结束字符(-1表示它转到字符串的结尾)。

© www.soinside.com 2019 - 2024. All rights reserved.