setting string width

H

hurcan solter

Hi there,Just a quick question ...
I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that
TIA
hurcan
 
A

Andrew Timberlake

Hurcan

The following adds a method to the string class called wrap which wraps a
string at the specified width broken on a space but honouring line endings

class String
def wrap(width)
gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end
end

s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas
cursus cursus enim. Ut sit amet metus at lacus consectetuer condimentum.
Suspendisse nulla arcu, varius eget, consequat id, sodales sit amet, turpis.
Etiam ornare felis id augue. Morbi eleifend sem nec odio. Aenean augue dui,
placerat et, porta tincidunt, accumsan vitae, tortor. Ut et nulla et diam
gravida eleifend. Nulla fermentum ligula nec tellus. Donec quis purus id
nisl blandit fermentum. Nunc commodo metus sit amet tortor. Nam lacus quam,
ultricies sit amet, fermentum at, dictum eget, metus."

puts s.wrap(80)

Andrew Timberlake
(e-mail address removed)
082 415 8283
skype: andrewtimberlake

"I have never let my schooling interfere with my education."
--Mark Twain


-----Original Message-----
From: hurcan solter [mailto:[email protected]]
Sent: 14 January 2008 11:01 AM
To: ruby-talk ML
Subject: setting string width

Hi there,Just a quick question ...
I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that
TIA
hurcan


!DSPAM:3,478b262e183241124252716!
 
B

Bill Kelly

From: "hurcan solter said:
Hi there,Just a quick question ...
I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that

Here's one way to do it:

string.gsub(/(.{75,}?\S*)\s+/, "\\1\n")

It meets the criteria of "around every 80ish character"... :)


Regards,

Bill
 
X

Xavier Noria

I'd like to insert newlines into a string around every 80ish
character and those newlines shouldnt cut the words in the middle. Can
anybody post a snippet how to do that

require 'facets'
str.word_wrap!
 
H

hurcan solter

yes yes the word was "the word wrap" .Silly me
thanks for the responses though..
 
R

Robert Dober

Ah, nice.
I know you have been served well but I would like to take advantage to
point to a RubyQuiz as there are surprisingly often
answers to Ruby programming questions. Yours was handled here
http://rubyquiz.com/quiz113.html

BTW James, will the Quiz page remain available after your retierement?

Cheers
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top