Return to Snippet

Revision: 18714
at October 6, 2009 13:57 by fuzzylollipop

Initial Code
// this adds a "truncate(#)" method to String and GString
String.metaClass.truncate = {len ->
    if (delegate == null) {return ''}
    if (delegate.length() > len) {return delegate[0..(len - 4)] + '...'}
    return delegate

Initial URL

Initial Description
this truncates a string to a provided length and adds "..." to the end to show that it was truncated.

Initial Title
Adding a truncate method to a String in Groovy

Initial Tags

Initial Language