Adding a truncate method to a String in Groovy
this truncates a string to a provided length and adds "..." to the end to show that it was truncated.
Copy this code and paste it in your HTML
// this adds a "truncate(#)" method to String and GString
String.
metaClass.
truncate = {len
-> if (delegate.
length() > len
) {return delegate
[0..
(len
- 4)] + '...'} }
Report this snippet
Comments
Subscribe to comments