Skip to content

Instantly share code, notes, and snippets.

@nickman
Created December 23, 2015 17:09
Show Gist options
  • Save nickman/ad1ffa4dc1f0a884d107 to your computer and use it in GitHub Desktop.
Save nickman/ad1ffa4dc1f0a884d107 to your computer and use it in GitHub Desktop.
Groovy to proper case underscore containing strings (i.e. DB column names) to convert to reasonable java/groovy field names
int US = '_';
uinds = { name ->
int index = 1;
int offset = 0;
int loops = 0;
uids = [];
while(index!=-1) {
index = name.indexOf(US, offset);
if(index!=-1) {
uids.add(index-loops);
offset += index+1;
}
loops++;
}
return uids as int[];
}
pc = { name ->
int[] u = uinds(name);
s = name.toLowerCase().replace("_", "");
if(u.length > 0) {
StringBuilder b = new StringBuilder(s);
u.each() { index ->
int nextChar = index+1;
if(nextChar <= b.length()) {
upchar = b.substring(index, nextChar).toUpperCase();
b.replace(index, index+1, upchar);
}
}
s = b.toString();
}
return s;
}
println pc("APEX_STANDARD_CONDITIONS");
println pc("APEX_STANDARD_CONDITIONS_PLUS");
println pc("APEX");
println pc("APEX_");
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment