Tuesday, March 1, 2011

Groovy convert underscored string to camel case

I cannot find a good convertor online. So I did it in groovy web console

http://groovyconsole.appspot.com/

static String toCamelCase( String text, boolean capitalized = false ) {
text = text.replaceAll( "(_)([A-Za-z0-9])", { Object[] it -> it[2].toUpperCase() } )
return capitalized ? capitalize(text) : text
}
static String toSnakeCase( String text ) {
text.replaceAll( /([A-Z])/, /_$1/ ).toLowerCase().replaceAll( /^_/, '' )
}
println toCamelCase("type_requisition")

The result is

typeRequisition

No comments:

Post a Comment