From: "Pål Bergström" Date: 2008-08-20T14:34:38+09:00 Subject: Re: Cut a string if length > n Robert Klemme wrote: > Pal, it seems we haven't seen the complete specification of what you > want to do. It seems that not only length is a condition but also > positions of spaces. > > I get the feeling that a solution using regular expressions might be > more efficient and also easier in your case. Maybe any of > > str.sub! %r{\A(.{50}).*\z}, '\\1' > str.sub! %r{\A(.{1,50})(?: .*)?\z}, '\\1' > str.sub! %r{\s\S*\z}, '' > > Depends on your string contents of course. > > Kind regards > > robert Hi. My app shows data in a list from a table called messages but only a fragment of it if it's to long. My solution seems to work fine. What do you think of it? Will it always work? lastspace = message.message[0..70].rindex(" ") puts message.message[0..lastspace.to_i] It doesn't do anything for non existing messages and text that isn't that long as 70. And it finds the last space. We might have long words in Swedish, but not that long :-) There will always be a space somewhere in the end where it can cut it without cutting and leave strange question marks as it cuts a swedish character (as Latin1 I guess). -- Posted via http://www.ruby-forum.com/.