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 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();
}
Regular expressions alone can't do that (there is no "touppercase" operator, so to speak).
But Guava has a nice little utility called CaseFormat
that can help you:
String result = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, str)
This works, even 'though your input is not strictly in UPPER_UNDERSCORE
format, but CaseFormat
is lenient this way (if you want the first character to be capitalized as well use UPPER_CAMEL
instead).
Alternatively, if you absolutely want to use regular expressions, you can use Matcher.appendReplacement
(it has a nice example in the JavaDoc):
public static final Pattern UNDERSCORE_FOLLOWED_BY_ANYTHING = Pattern
.compile("_(.)");
public static String toUpperAfterUnderscore(String input) {
Matcher m = UNDERSCORE_FOLLOWED_BY_ANYTHING.matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
return sb.toString();
}
You can also try splitting.
String str = "a_bcde_fghij_k"
String result[] = str.split("_");
String newstr = result[0];
for (int i=1;i<result.length;i++) {
char first = Character.toUpperCase(result[i].charAt(0));
newstr = newstr + first + result[i].substring(1);
}
System.out.println(newstr);
split() takes regex, if you feel that is important.
A slightly different approach from me but works good..
String str = "a_bcde_fghij_k";
int count=0;
String[] splitString = (str.split("_"));
for (String string : splitString)
{
count++;
if(count>1)
{
char c= string.charAt(0);
System.out.print(string.replace(c, Character.toUpperCase(c)));
}
else
System.out.print(string);
}
Isn't it?
There's no straight away way to do it with regexes, but I think using regexes can greatly simplify the task:
public static String underscoreToCapital(final String input) {
String ret = input;
final Matcher m = Pattern.compile("_([a-z])").matcher(input);
while (m.find()) {
final String found = m.group();
final String toUppercase = m.group(1);
ret = ret.replaceAll(found, toUppercase.toUpperCase());
}
return ret;
}
You have to do probably some check on the execution of this method but this could be another idea:
public String replaceAndUpper(String word) {
int charToRemove = word.indexOf("_");
while (charToRemove != -1) {
String part1 = word.substring(0, charToRemove);
String part2 = word.substring(charToRemove + 1);
char upperChar = Character.toUpperCase(part2.charAt(0));
word = part1 + String.valueOf(upperChar) + part2.substring(1);
charToRemove = word.indexOf("_");
}
return word;
}