Here's something I'm using. It can probably be done much more easily,
but I'm to busy to make it elegant. You can see what it's doing. I have
a minimum length, and I break at word boundaries. There's a lot of
improvements that could be made. For example, it doesn't recognize
punctuation, but it should.
/** Shorten string to max length and replace last three chars with an
ellipsis */
def shorten(in: String, len: Int): String = {
if (in.length > (10 max len)) {
val words = in.substring(0,(7 max (len - 3))).split(" ").toList
if (words.length > 1)
words.dropRight(1).reduceLeft(_ + " " + _) + "..."
else if (words.head.length > (7 max len))
words.head.substring(0, (7 max len)) + "..."
else
words.head + "..."
}
else
in
}
Chas.
David Pollak wrote:
> We don't have one.
>
> Please write one up and add it to Helpers for 1.1.
>
> On Wed, Feb 18, 2009 at 4:46 PM, Tim Perrett <[email protected]
> <mailto:[email protected]>> wrote:
>
>
> Guys,
>
> Just a quick one... looking for a similar method to:
>
> http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M001711
>
> I know i could knock one up, but just wanted to check nothing like
> this exists already as it seems like a fairly util type task. I had a
> look but couldn't see anything in master.
>
> Cheers, Tim
>
>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Beginning Scala http://www.apress.com/book/view/1430219890
> Follow me: http://twitter.com/dpp
> Git some: http://github.com/dpp
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---