Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: recompose strings of limited length Date: Tue, 20 Dec 2016 19:16:52 +0100 Lines: 36 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net 09phpl+pZKXF99BtBOU3wAgkljgl25+ZeM4Ij4ygLtf6cHfwM= Cancel-Lock: sha1:aqRf8GWYjON+4sAv9LeNx+jEMcE= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 In-Reply-To: Xref: csiph.com comp.lang.ruby:7313 On 19.12.2016 17:19, Michael Uplawski wrote: > What I *want to* do (nobody asks me): take a string of arbitrary > length, or an array of such random strings and modify them in a way > that, in the result, all strings are split up into chunks of a maximum > string-length. max_len =3D 10 strings =3D str.scan /.{1,#{max_len}}/ > ----------- > me@here:/tmp$ ./test.rb "Ein K=C3=B6nig mit Krone ist besser als ohne" = 15 > Ein K=C3=B6nig mit > Krone ist > besser als > ohne > ----------- You never mentioned that you want word splitting. irb(main):011:0> str =3D "Ein K=C3=B6nig mit Krone ist besser als ohne" =3D> "Ein K=C3=B6nig mit Krone ist besser als ohne" irb(main):015:0> str.scan %r{\S.{0,#{max_len - 1}}(?!\S)} =3D> ["Ein K=C3=B6nig mit", "Krone ist", "besser als ohne"] It seems your last two strings should be one. Cheers robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/