I have a String str = "a_bcde_fghij_k".
and I want to change it to "aBcdeFghijK"
If have a _ character, the next character will be change to uppercase and remove _ character.
How can I do this?
I suspect you'll need to just go through this character by character, building up the string as you go. For example:
public static String underscoreToCapital(String text) {
// This will be a bit bigger than necessary, but that shouldn't matter.
StringBuilder builder = new StringBuilder(text.length());
boolean capitalizeNext = false;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '_') {
capitalizeNext = true;
} else {
builder.append(capitalizeNext ? Character.toUpperCase(c) : c);
capitalizeNext = false;
}
}
return builder.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With