Created
February 14, 2010 00:06
-
-
Save ggayan/303760 to your computer and use it in GitHub Desktop.
Grails codec that allows to manipulate underscores inside strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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