Cut a string each 80 character

Hello everyone!

I want to do cut a string each 80 character. Do you know how to do?

For instance :
string = “jdskj fjkjjkl
djskfljksldjfkjdkljksljkdjfkdlsjfklsjdkfldsjdkfljkldjksjfkljkdlfjkdsljkljfdklskjkdfldfldfjkdfjkldfskjldfskjldfkjldfskjlsdfkjldfskljdfskjldkjldfkjdfkllkdf”

For this string, I want to add
each 80 character for the third
word.

Thank you :slight_smile:

Ooops!
Replace “each” by “every”

Sorry!

For instance :
string = “jdskj fjkjjkl
djskfljksldjfkjdkljksljkdjfkdlsjfklsjdkfldsjdkfljkldjksjfkljkdlfjkdsljk
ljfdklskjkdfldfldfjkdfjkldfskjldfskjldfkjldfskjlsdfkjldfskljdfskjldkjld
fkjdfkllkdf”

string.scan(%r{.{1,80}})

or

n = 80
string.scan(%r{.{1,#{n}})

For this string, I want to add
each 80 character for the third
word.

string.scan(%r{.{1,80}}).map { |_chunk|
_chunk + ‘

}

Make corresponding adjustments if you want to apply it to a particular
word of your string (e.g., do stirng.split.slice(2) for the third word).

Hope it helps,
Gennady.

You may need to adjust the regex a little, but that should match 8
charicters at a time…
I think if the length isn’t a multiple of 8 it may just ignore the
last bunch of chars… but you get the idea

For that not to be ignored, use {1,8} instead of just {8}. As a minor
note, OP wanted 80, not 8 :wink:

Gennady.

On Wed, Mar 4, 2009 at 8:06 PM, Guillaume L. [email protected]
wrote:

Ooops!
Replace “each” by “every”

Sorry!

I’m thinking you want scan.

string.scan(/.{8}/)

You may need to adjust the regex a little, but that should match 8
charicters at a time…
I think if the length isn’t a multiple of 8 it may just ignore the
last bunch of chars… but you get the idea

edit the regex as needed.

PS: Scan is awesome when you need to do some quick an dirty log
analysis
data=File.read(foo).scan(regex)

Isn’t that pretty much the same as:

string.gsub(%r{.{1,80}}, “\0
”)

leo.

Almost. The latter produces a string with “
” after every 80 chars,
while the former returns an array of strings 86 chars long each. What is
more appropriate depends on a context. Say, you may want to decorate
chunks even further by passing them through some other methods (on the
other hand, grep with a block may be used for that as well).

The latter may be faster, though.

Thanks for bringing it up,
Gennady.

string.scan(%r{.{1,80}}).map { |_chunk|
_chunk + ‘

}

Isn’t that pretty much the same as:

string.gsub(%r{.{1,80}}, “\0
”)

leo.

On 05.03.2009 12:32, lasitha wrote:

ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin8.11.1]
user system total real
with_scan 0.750000 0.040000 0.790000 ( 0.851104)
with_gsub 0.840000 0.060000 0.900000 ( 0.950050)
with_insert 2.000000 0.050000 2.050000 ( 2.210962)

There is an inconsistency in your test: the gsub version creates the
second argument to gsub over and over again. You should rather define a
constant for that as well. You could do that as well for the sanity
check string.

With the attached changed script this is what I get:

[robert@ora01 ~]$ allruby bm.rb
ruby 1.8.5 (2006-08-25) [i386-linux]
Rehearsal -----------------------------------------------
with_scan 1.770000 0.040000 1.810000 ( 1.821903)
with_gsub 1.720000 0.010000 1.730000 ( 1.750455)
with_insert 9.230000 0.000000 9.230000 ( 9.228760)
------------------------------------- total: 12.770000sec

               user     system      total        real

with_scan 1.730000 0.010000 1.740000 ( 1.741069)
with_gsub 1.730000 0.010000 1.740000 ( 1.741975)
with_insert 9.130000 0.000000 9.130000 ( 9.130356)
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
Rehearsal -----------------------------------------------
with_scan 1.040000 0.010000 1.050000 ( 1.043332)
with_gsub 1.100000 0.010000 1.110000 ( 1.120970)
with_insert 9.030000 0.000000 9.030000 ( 9.077018)
------------------------------------- total: 11.190000sec

               user     system      total        real

with_scan 1.030000 0.000000 1.030000 ( 1.031019)
with_gsub 1.090000 0.010000 1.100000 ( 1.105150)
with_insert 9.040000 0.000000 9.040000 ( 9.049539)
[robert@ora01 ~]$

CentOS 5.2 in VM.

Kind regards

robert

On Thu, Mar 5, 2009 at 2:00 PM, Gennady B.
[email protected] wrote:

string.gsub(%r{.{1,80}}, “\0
”)

[…]
The latter may be faster, though.

Doesn’t appear to be.

I threw in a primitive insert loop for comparison and was a little
surprised to find it considerably slower than both the others.

ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin8.11.1]
user system total real
with_scan 0.750000 0.040000 0.790000 ( 0.851104)
with_gsub 0.840000 0.060000 0.900000 ( 0.950050)
with_insert 2.000000 0.050000 2.050000 ( 2.210962)

Solidarity,
lasitha.

On Thu, Mar 5, 2009 at 6:03 PM, Robert K.
[email protected] wrote:

On 05.03.2009 12:32, lasitha wrote:

Doesn’t appear to be.
Benchmarking ways to insert a separator at every 80 characters of a string · GitHub

There is an inconsistency in your test: the gsub version creates the second
argument to gsub over and over again. You should rather define a constant
for that as well. You could do that as well for the sanity check string.

Yes, thank you for the pointers. In this case the change didn’t seem
to make any significant difference on my box, but the wisdom is
gratefully accepted.

Cheers,
lasitha.