Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chop a string in Ruby into fixed length string ignoring (not considering/regardless) new line or space characters

Tags:

string

split

ruby

I have a string containing many new line and spaces. I need to split it into fixed length sub strings. E.g

a = "This is some\nText\nThis is some text"

And now i would like to split it into say strings of length 17. so now it should result in

["This is some\nText", "\nThis is some tex", "t"]

Comment: My string may contain any character (white space/word etc)

like image 480
user2767858 Avatar asked Sep 11 '13 08:09

user2767858


1 Answers

"This is some\nText\nThis is some text".scan(/.{1,17}/m)
# => ["This is some\nText", "\nThis is some tex", "t"]
like image 192
sawa Avatar answered Nov 15 '22 07:11

sawa