將字串轉成駝峰式形式字串
Example:
"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"
解題想法:
import java.lang.StringBuilder;
class Solution{
static String toCamelCase(String s){
String[] splitStr=s.split("[-,_]");
String resultStr="";
for(int i=1;i<splitStr.length;i++){
splitStr[i]=splitStr[i].toUpperCase().charAt(0) + splitStr[i].substring(1);
}
for(int i=0;i<splitStr.length;i++){
resultStr+=splitStr[i];
}
return resultStr;
}
}
其它提供解法
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.lang.StringBuilder;
class Solution{
static String toCamelCase(String s){
Matcher m = Pattern.compile("[_|-](\\w)").matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
return m.appendTail(sb).toString();
}
}