开发者

Function to add line breaks to a string every so many characters?

Rails 2.3.5

I need a 开发者_如何转开发temp work around for a script that only has 1 possible styling. Is there a simple way to loop through a string and add line breaks? Like at every 80th character insert a '\n'? (really looping through a record set and doing this to a text field).

Thanks!


Here is another approach:

"hello".scan(/.{1}/).join("\n")


Not sure this is the best approach.. but you could use each_slice here. Maybe something like:

"SOME AWESOME STRING".chars.each_slice(80).map(&:join).join('\n')
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜