Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Last active August 29, 2015 13:56
Show Gist options
  • Save jarrodhroberson/8995331 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/8995331 to your computer and use it in GitHub Desktop.
Truncate String in Groovy
// this adds a "truncate(#)" method to String
String.metaClass.truncate = { len ->
if (delegate == null) {return''}
if (delegate.length() > len) {return delegate[0..(len - 2)] + "\u2026"}
return delegate
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment