Skip to content

Instantly share code, notes, and snippets.

@ggayan
Created February 14, 2010 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggayan/303760 to your computer and use it in GitHub Desktop.
Save ggayan/303760 to your computer and use it in GitHub Desktop.
Grails codec that allows to manipulate underscores inside strings
//codec must be created inside grails-app/utils named like *Codec.groovy
class UnderscoreCodec {
static encode = {target->
target.replaceAll(" ", "_")
}
static decode = {target->
target.replaceAll("_", " ")
}
}
//define a test in test/unit
class UnderscoreCodecTests extends GroovyTestCase {
void testEncode() {
String test = "this is a test"
assertEquals "this_is_a_test", test.encodeAsUnderscore()
}
void testDecode() {
String test = "this_is_a_test"
assertEquals "this is a test", test.decodeUnderscore()
}
}
//credits to https://www.ibm.com/developerworks/java/library/j-grails03109/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment