Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #7313

Re: recompose strings of limited length

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: recompose strings of limited length
Date 2016-12-20 19:16 +0100
Message-ID <ebtaslFd966U1@mid.individual.net> (permalink)
References <slrno5g259.idu.michael.uplawski@drusus.uplawski.eu>

Show all headers | 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 | NextPrevious in thread | Next in thread | Find similar


Thread

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