美文网首页kata
每日kata~02-camel case-[github搬家]

每日kata~02-camel case-[github搬家]

作者: Lacia | 来源:发表于2020-04-29 15:58 被阅读0次

    题目

    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;
        }

    相关文章

      网友评论

        本文标题:每日kata~02-camel case-[github搬家]

        本文链接:https://www.haomeiwen.com/subject/fhcnwhtx.html