将Ruby中的字符串切换为固定长度的字符串,忽略(不考虑/不考虑)新的行或空格字符

use*_*858 5 ruby string split

我有一个包含许多新行和空格的字符串.我需要将其拆分为固定长度的子字符串.例如

a = "This is some\nText\nThis is some text"
Run Code Online (Sandbox Code Playgroud)

现在我想把它分成长度为17的字符串.所以现在它应该导致

["This is some\nText", "\nThis is some tex", "t"]
Run Code Online (Sandbox Code Playgroud)

评论:我的字符串可能包含任何字符(空格/单词等)

saw*_*awa 7

"This is some\nText\nThis is some text".scan(/.{1,17}/m)
# => ["This is some\nText", "\nThis is some tex", "t"]
Run Code Online (Sandbox Code Playgroud)