1、需求:判断字符串是否由大小写组合,如是,则在小写与大写之间加下划线 2、实现 public static void main(String[] args) { //字符串 String key="iterName"; //小写个数 int lower=0; //大写个数 int capit=0; //混合中,大写开始的下标 int blend=0; for(int i = 0; i < key.length(); i++){ char c = key.charAt(i); if(Character.isLowerCase(c)){//小写 lower=lower+1;//小写个数+1 }else { capit=capit+1; //大写个数+1 if(capit==1) { blend=i; } } } if(lower<key.length()&&capit<key.length()){ //大写和小写的个数都小于总数,说明既有大写又有小写 key=key.substring(0,blend)+"_"+key.substring(blend); } System.out.println(key); } 3、效果 (1)纯小写 (2)纯大写 (3)小写、大写混合(小写与大写之间用“_”拼接) PS:此处不考虑大写在前、小写在后(如ITERname)的情况:Java的命名规范中变量采用的是驼峰式命名法,即一个单词时,全部小写(name),多个单词时,从第二个开始,首字母大写(userName、userEnglishName)等