将 Ruby 中的字符串切成固定长度的字符串,忽略(不考虑/不管)新行或空格字符 [英] Chop a string in Ruby into fixed length string ignoring (not considering/regardless) new line or space characters
本文介绍了将 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"
现在我想把它分成长度为 17 的字符串.所以现在它应该导致
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)
推荐答案
"This is some\nText\nThis is some text".scan(/.{1,17}/m)
# => ["This is some\nText", "\nThis is some tex", "t"]
这篇关于将 Ruby 中的字符串切成固定长度的字符串,忽略(不考虑/不管)新行或空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文