Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7313
| Path | csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.ruby |
| Subject | Re: recompose strings of limited length |
| Date | Tue, 20 Dec 2016 19:16:52 +0100 |
| Lines | 36 |
| Message-ID | <ebtaslFd966U1@mid.individual.net> (permalink) |
| References | <slrno5g259.idu.michael.uplawski@drusus.uplawski.eu> |
| 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 | <slrno5g259.idu.michael.uplawski@drusus.uplawski.eu> |
| Xref | csiph.com comp.lang.ruby:7313 |
Show key headers only | View raw
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 = 10
strings = str.scan /.{1,#{max_len}}/
> -----------
> me@here:/tmp$ ./test.rb "Ein König mit Krone ist besser als ohne" 15
> Ein König mit
> Krone ist
> besser als
> ohne
> -----------
You never mentioned that you want word splitting.
irb(main):011:0> str = "Ein König mit Krone ist besser als ohne"
=> "Ein König mit Krone ist besser als ohne"
irb(main):015:0> str.scan %r{\S.{0,#{max_len - 1}}(?!\S)}
=> ["Ein König mit", "Krone ist", "besser als ohne"]
It seems your last two strings should be one.
Cheers
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar
recompose strings of limited length Michael Uplawski <michael.uplawski@uplawski.eu> - 2016-12-19 17:19 +0100
Re: recompose strings of limited length Robert Klemme <shortcutter@googlemail.com> - 2016-12-20 19:16 +0100
Re: recompose strings of limited length Michael Uplawski <michael.uplawski@uplawski.eu> - 2016-12-21 10:55 +0100
Re: recompose strings of limited length Robert Klemme <shortcutter@googlemail.com> - 2016-12-21 19:02 +0100
Re: recompose strings of limited length Michael Uplawski <michael.uplawski@uplawski.eu> - 2016-12-22 15:34 +0100
Re: recompose strings of limited length Robert Klemme <shortcutter@googlemail.com> - 2016-12-24 18:37 +0100
Re: recompose strings of limited length "Robert L." <No_spamming@noWhere_7073.org> - 2017-01-15 14:10 +0000
csiph-web