题目
Convert string to camel case
Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized.
e.g:
// returns "theStealthWarrior" toCamelCase("the-stealth-warrior")
// returns "TheStealthWarrior" toCamelCase("The_Stealth_Warrior")
最优解应该是用的正则,稍后再研究一下
public static String toCamelCase(String s){
char[] c = s.toCharArray();
String res = "";
char flag = '-';
if(s.length() == 0)
return "";
res += c[0];
for(int i=1;i<c.length;i++) {
if((c[i]=='-')||(c[i])=='_') {
res += Character.toUpperCase(c[i+1]);
i += 1;
}
else
res += c[i];
}
return res;
}
网友评论