[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

truncate a string to first 200 characters

luke

7/9/2005 3:16:00 AM


hi,

i've been searching high and low for a method that will help me do this for
a couple of days, but decided eventually just to post here in the hope
someone can point out the probably blatantly obvious for me :)

i'm looking for something that can take a string and truncate it to a
certain amount of characters. then i can add '...' at the end.

i used to use substr() when working with php

$str = substr( $str, 0, 200) . "...";

many thanks
luke


5 Answers

daz

7/9/2005 3:25:00 AM

0


luke wrote:
>
> hi,
>
> i've been searching high and low for a method that will help me do this for
> a couple of days, but decided eventually just to post here in the hope
> someone can point out the probably blatantly obvious for me :)
>
> i'm looking for something that can take a string and truncate it to a
> certain amount of characters. then i can add '...' at the end.
>

s = 'abcdefghijklmnopqrstuvwxyz'
puts s[0, 6] << '...'

#-> abcdef...


daz



Florian Groß

7/9/2005 3:34:00 AM

0

nobu.nokada

7/9/2005 3:41:00 AM

0

Hi,

At Sat, 9 Jul 2005 12:25:50 +0900,
daz wrote in [ruby-talk:147632]:
> s = 'abcdefghijklmnopqrstuvwxyz'
> puts s[0, 6] << '...'
>
> #-> abcdef...

If you consider multi-byte environments, you should strip
trailing garbage bytes.

puts s[0, 6][/\A.*/m] << '...'

--
Nobu Nakada


luke

7/9/2005 4:15:00 AM

0


aaaah. i see. is this another case of 'everything is an object'? i need to
wrap my head around the all pervasiveness of what that means in the future.
thanks all.



"Florian Groß" <florgro@gmail.com> wrote in message
news:danghf$l0b$1@sea.gmane.org...
> luke wrote:
>
> > i'm looking for something that can take a string and truncate it to a
> > certain amount of characters. then i can add '...' at the end.
> >
> > i used to use substr() when working with php
> >
> > $str = substr( $str, 0, 200) . "...";
>
> The most correct way of doing this IMHO is this:
>
> str += "..." if str.slice!(200 .. -1)
>
> which will cut away everything after the first 200 characters and append
> a marker if the string was truncated.
>
> Your PHP solution would map to this:
>
> str = str[0, 200] + "..."
>
>
>


Devin Mullins

7/9/2005 4:53:00 AM

0

luke wrote:

>aaaah. i see. is this another case of 'everything is an object'? i need to
>wrap my head around the all pervasiveness of what that means in the future.
>thanks all.
>
>
"blah".class #=> String

From the command line:
ri String#[]
ri String#concat
ri String#slice!

Generally,
ri String

Devin

>>The most correct way of doing this IMHO is this:
>>
>>str += "..." if str.slice!(200 .. -1)
>>
>>which will cut away everything after the first 200 characters and append
>>a marker if the string was truncated.
>>
>>Your PHP solution would map to this:
>>
>>str = str[0, 200] + "..."
>>
>>