开发者

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

开发者 https://www.devze.com 2023-03-18 08:59 出处:网络
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 i

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

精彩评论

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

关注公众号