better usage of StringBuilder in casing

This commit is contained in:
kimchy 2010-04-09 07:38:27 +03:00
parent daac94092c
commit 3a59397306

View File

@ -1166,12 +1166,20 @@ public class Strings {
} }
public static String toCamelCase(String value) { public static String toCamelCase(String value) {
StringBuilder sb = new StringBuilder(); return toCamelCase(value, null);
}
public static String toCamelCase(String value, StringBuilder sb) {
boolean changed = false; boolean changed = false;
for (int i = 0; i < value.length(); i++) { for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i); char c = value.charAt(i);
if (c == '_') { if (c == '_') {
if (!changed) { if (!changed) {
if (sb != null) {
sb.setLength(0);
} else {
sb = new StringBuilder();
}
// copy it over here // copy it over here
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
sb.append(value.charAt(j)); sb.append(value.charAt(j));
@ -1192,12 +1200,20 @@ public class Strings {
} }
public static String toUnderscoreCase(String value) { public static String toUnderscoreCase(String value) {
StringBuilder sb = new StringBuilder(); return toUnderscoreCase(value, null);
}
public static String toUnderscoreCase(String value, StringBuilder sb) {
boolean changed = false; boolean changed = false;
for (int i = 0; i < value.length(); i++) { for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i); char c = value.charAt(i);
if (Character.isUpperCase(c)) { if (Character.isUpperCase(c)) {
if (!changed) { if (!changed) {
if (sb != null) {
sb.setLength(0);
} else {
sb = new StringBuilder();
}
// copy it over here // copy it over here
for (int j = 0; j < i; j++) { for (int j = 0; j < i; j++) {
sb.append(value.charAt(j)); sb.append(value.charAt(j));