Mark
Mark

Reputation: 69920

Extending Groovy String class

Groovy allows to do some nice things with strings in frontend pages, like:

${"hello".capitalize()}

How can I add a new custom method to the String class? Like:

${"hello".custom()}

Upvotes: 6

Views: 1058

Answers (1)

MarcDeXeT
MarcDeXeT

Reputation: 264

Use the metaClass

String.metaClass.custom = { //dosomething }

See http://www.groovyexamples.org/2010/07/19/dynamically-add-properties-to-a-class/

Upvotes: 7

Related Questions