[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Cut a string if length > n

Pål Bergström

8/19/2008 3:25:00 PM

What's the best way to cut a string if the length is above n characters?
Is it slice, or is there any other convenient method? Trying to
understand the string class from the docs but not sure which one to use.
--
Posted via http://www.ruby-....

14 Answers

ara.t.howard

8/19/2008 3:46:00 PM

0


On Aug 19, 2008, at 9:25 AM, P=E5l Bergstr=F6m wrote:

> What's the best way to cut a string if the length is above n =20
> characters?
> Is it slice, or is there any other convenient method? Trying to
> understand the string class from the docs but not sure which one to =20=

> use.


this_always_works =3D string[ 0 .. n ]

regardless of whether the string is <, =3D, or > than n

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Robert Klemme

8/19/2008 4:12:00 PM

0

On 19.08.2008 17:45, ara.t.howard wrote:
> On Aug 19, 2008, at 9:25 AM, Pål Bergström wrote:
>
>> What's the best way to cut a string if the length is above n
>> characters?
>> Is it slice, or is there any other convenient method? Trying to
>> understand the string class from the docs but not sure which one to
>> use.
>
>
> this_always_works = string[ 0 .. n ]
>
> regardless of whether the string is <, =, or > than n

I believe slice! also always works:

irb(main):011:0> s="abcdefghijklmnop"
=> "abcdefghijklmnop"
irb(main):012:0> s.length
=> 16
irb(main):013:0> s.slice! 50..-1
=> nil
irb(main):014:0> s
=> "abcdefghijklmnop"
irb(main):015:0> s.slice! 5..-1
=> "fghijklmnop"
irb(main):016:0> s
=> "abcde"

It looks uglier but it modifies the string in place which might come in
handy at times.

Kind regards

robert

Pål Bergström

8/19/2008 5:02:00 PM

0

Ara Howard wrote:
> On Aug 19, 2008, at 9:25 AM, P�l Bergstr�m wrote:
>
>> What's the best way to cut a string if the length is above n
>> characters?
>> Is it slice, or is there any other convenient method? Trying to
>> understand the string class from the docs but not sure which one to
>> use.
>
>
> this_always_works = string[ 0 .. n ]
>
> regardless of whether the string is <, =, or > than n
>
> a @ http://codeforp...

Found a problem with this. It messes up swedish letters that, I guess,
is cut right of as if they where handled as latin1. What method can
handle utf-8?
--
Posted via http://www.ruby-....

Pål Bergström

8/19/2008 5:43:00 PM

0

Pål Bergström wrote:

Don't say I've run into a classic encoding issue. :-(

Does Ruby have a problem with utf8 support?
--
Posted via http://www.ruby-....

Rob Biedenharn

8/19/2008 7:37:00 PM

0


On Aug 19, 2008, at 1:42 PM, P=E5l Bergstr=F6m wrote:

> P=E5l Bergstr=F6m wrote:
>
> Don't say I've run into a classic encoding issue. :-(
>
> Does Ruby have a problem with utf8 support?
> --=20
> Posted via http://www.ruby-....
>


I did this for a project (way back in Rails 1.1.6)

$KCODE =3D 'UTF8'
require 'jcode'

class String
def first(limit =3D 1)
self.match(%r{^(.{0,#{limit}})})[1]
end
end

You'd still have to assign this, but:

irb> name =3D "P\303\245l Bergstr\303\266m"
=3D> "P\303\245l Bergstr\303\266m"
irb> puts name
P=E5l Bergstr=F6m
=3D> nil
irb> puts name.first(2)
P=E5
=3D> nil
irb> puts name.first(3)
P=E5l
=3D> nil
irb> puts name.first(12)
P=E5l Bergstr=F6
=3D> nil
irb> puts name[0...12]
P=E5l Bergstr
=3D> nil
irb> puts name[0...2]
P?
=3D> nil
irb> p name[0...2]
"P\303"
=3D> nil

I hope that helps you.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com=

Pål Bergström

8/19/2008 8:08:00 PM

0

This seems to do the trick. Will it always work?

lastspace = message.message[0..70].rindex(" ")
puts message.message[0..lastspace.to_i]
--
Posted via http://www.ruby-....

Mark Thomas

8/19/2008 8:16:00 PM

0

On Aug 19, 11:25 am, Pål Bergström <p...@palbergstrom.com> wrote:
> What's the best way to cut a string if the length is above n characters?
> Is it slice, or is there any other convenient method? Trying to
> understand the string class from the docs but not sure which one to use.

Rails has a utf8-compatible helper called truncate, called like so:

truncate(text, length = 30, truncate_string = "...")
If text is longer than length, text will be truncated to the length of
length (defaults to 30) and the last characters will be replaced with
the truncate_string (defaults to "...").

And the implementation is:

def truncate(text, length = 30, truncate_string = "...")
if text
l = length - truncate_string.chars.length
chars = text.chars
(chars.length > length ? chars[0...l] + truncate_string :
text).to_s
end
end

Where chars is a string method in Ruby 1.8.7 or greater.


Pål Bergström

8/19/2008 8:22:00 PM

0

Mark Thomas wrote:

> Rails has a utf8-compatible helper called truncate, called like so:
>
> truncate(text, length = 30, truncate_string = "...")
> If text is longer than length, text will be truncated to the length of
> length (defaults to 30) and the last characters will be replaced with
> the truncate_string (defaults to "...").
>
> And the implementation is:
>
> def truncate(text, length = 30, truncate_string = "...")
> if text
> l = length - truncate_string.chars.length
> chars = text.chars
> (chars.length > length ? chars[0...l] + truncate_string :
> text).to_s
> end
> end
>
> Where chars is a string method in Ruby 1.8.7 or greater.

Perfect :-)
--
Posted via http://www.ruby-....

Rob Biedenharn

8/19/2008 8:33:00 PM

0

On Aug 19, 2008, at 4:08 PM, P=E5l Bergstr=F6m wrote:

> This seems to do the trick. Will it always work?
>
> lastspace =3D message.message[0..70].rindex(" ")
> puts message.message[0..lastspace.to_i]


You likely want an exclusive range unless you want the space at the end.

message.message[0...lastspace.to_i]

What if there's no space? Then perhaps:

message.message[0..(lastspace || -1).to_i]

Of course, that puts you back in the position of getting the full =20
string rather than some truncated version with no more than your =20
desired number of characters.

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Robert Klemme

8/20/2008 5:20:00 AM

0

On 19.08.2008 22:33, Rob Biedenharn wrote:
> On Aug 19, 2008, at 4:08 PM, Pål Bergström wrote:
>
>> This seems to do the trick. Will it always work?
>>
>> lastspace = message.message[0..70].rindex(" ")
>> puts message.message[0..lastspace.to_i]
>
>
> You likely want an exclusive range unless you want the space at the end.
>
> message.message[0...lastspace.to_i]
>
> What if there's no space? Then perhaps:
>
> message.message[0..(lastspace || -1).to_i]
>
> Of course, that puts you back in the position of getting the full
> string rather than some truncated version with no more than your
> desired number of characters.

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