A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

/*
* 将"goOd gooD stUdy dAy dAy up"每个单词的首字母转换成大写其余还是小写字母
* */
public class DayDayUp {
        public static void main(String[] args) {
                String s ="goOd gooD stUdy dAy dAy up";
                //拆分字符串
                String[] b =s.split(" ");
                for (int i = 0; i < b.length; i++) {
                        for (int j = 0; j < b[i].length(); j++) {
                                if(j==0){
                                        //得到第一位字符,转成String类型,再转大写
                                        System.out.print((b[i].charAt(j)+"").toUpperCase());
                                }else{
                                        System.out.print((b[i].charAt(j)+"").toLowerCase());
                                }
                        }
                        System.out.print(" ");
                }
        }
}

说说其他解法

2 个回复

倒序浏览
public static void main(String[] args) {
                String s = "goOd gooD stUdy dAy dAy up";
                String[] str =s.split(" ");
                String k = "";
                for (int i = 0; i < str.length; i++) {
                        String f  = str[i];
                        k +=f.substring(0,1).toUpperCase().concat(f.substring(1).toLowerCase());
                }
                System.out.println(k);
        }
回复 使用道具 举报
15835811325 发表于 2016-9-16 23:37
public static void main(String[] args) {
                String s = "goOd gooD stUdy dAy dAy up";
                String[] str = ...

可以,时间复杂度少了平方了。
后面还需要concat("  ")哟
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马