codewars——java(2)

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.

解决方法一:

思路:以下划线或中划线拆分字符串,将首字母转换大写之后再拼接。

import java.lang.StringBuilder;

class Solution{


  static String toCamelCase(String s){

  String[] aa;

    String cc="";

    String bb;

  if(s.contains("-")){

    aa= s.split("-");

    cc= aa[0];

      for(int i = 1;i<aa.length;i++){

  bb=aa[i].substring(0,1).toUpperCase()+aa[i].substring(1);

  cc=cc+bb;

  }


  }

else if(s.contains("_")){

  aa = s.split("_");

  cc = aa[0];

    for(int i = 1;i<aa.length;i++){

  bb=aa[i].substring(0,1).toUpperCase()+aa[i].substring(1);

  cc=cc+bb;

  }


  }

    return cc;

  }

}


https://www.codewars.com/kata/517abf86da9663f1d2000003/train/java

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容