You can use the CaseFormat
class's LOWER_UNDERSCORE
from google Guava :
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "my_name")
Pergunta
I Need to change String : underbar + lowercase = uppercase.(and the opposite)
my_name -> myName
Is there any library or something to help this out?
Solução
You can use the CaseFormat
class's LOWER_UNDERSCORE
from google Guava :
CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "my_name")
Outras dicas
I suggest a custom solution
Pattern p = Pattern.compile("_(.)");
Matcher m = p.matcher("my_name");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
System.out.println(sb.toString());
output
myName
CaseFormat
is an utility class in Google Guava for converting between case conventions.
Another solution is to use StringTokenizer:
String value = "id_app";
StringTokenizer toekn = new StringTokenizer(value,"_");
StringBuilder str = new StringBuilder(toekn.nextToken());
while (toekn.hasMoreTokens()) {
String s = toekn.nextToken();
str.append(Character.toUpperCase(s.charAt(0))).append(s.substring(1));
}
System.out.println(str.toString());
Check this code, I have verified it.
String str = new String("my_name");
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)=='_' && (int) str.charAt(i+1)>=97 && (int) str.charAt(i+1)<=122){
str=str.replace(str.substring(i, i+2),""+(char)((int) str.charAt(i+1)-32));
}
}
System.out.println(str);
String s="My_name";
int i=s.indexOf('_');
s=s.replaceFirst(Character.toString(s.charAt(i+1)),Character.toString(Character.toUpperCase(s.charAt(i+1))));
s=s.replaceAll("_","");
String s="srinivas";
s=s.replaceFirst(Character.toString(s.charAt(0)),
Character.toString(Character.toUpperCase(s.charAt(0))));
//s Value is "Srinivas" now
You can replace lower case character following underscore with uppercase using appendReplacement
The appendReplacement and appendTail methods can be used in tandem in order to collect the result into an existing string buffer
String text = "my_name_is";
Matcher m = Pattern.compile("([_][a-z])").matcher(text);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group().substring(1).toUpperCase());
}
m.appendTail(sb);