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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘文秀 中级黑马   /  2016-7-29 23:32  /  716 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

* 6.将"goOd gooD stUdy dAy dAy up"
* 每个单词的首字母转换成大写其余还是小写字母(不许直接输出good good study day day up 要用代码实现)

11 个回复

倒序浏览
String s= "goOd gooD stUdy dAy dAy up";
s= s.toLowerCase();
s=s.insert(0,"G");
s=s.insert(5,"G");
...
后面的都是调用这个方法。。。。

评分

参与人数 1黑马币 +3 收起 理由
r1503882525 + 3 赞一个!

查看全部评分

回复 使用道具 举报
说的好有道理
回复 使用道具 举报
package com.itheima;

public class Demo2 {
        /*将"goOd gooD stUdy dAy dAy up"
    每个单词的首字母转换成大写其余还是小写字母
    (不许直接输出good good study day day up 要用代码实现)*/
        public static void main(String[] args) {
                String str = "goOd gooD stUdy dAy dAy up";
                //使用空格切割字符串,把切割出来的字符串放到集合中
                String[] arr = str.split(" ");
                //循环字符串数组
                for(int i =0;i<arr.length;i++){
                        //遍历集合中的元素的字符
                        for(int j =0;j<arr[i].length();j++){
                                //判断数组中的元素的第一个字符,直接把第一个字符调用touppercase方法大写
                                if(j==0){
                                        System.out.print((arr[i].charAt(j)+"").toUpperCase());
                                //如果不是第一个字符,则调用转换小写方法
                                }else{
                                        System.out.print((arr[i].charAt(j)+"").toLowerCase());
                                }
                        }
                        //使每一个数组中的字符串元素用空格隔开,输出美观
                        System.out.print("  ");
                }
        }
}
回复 使用道具 举报
cat73 黑马帝 2016-7-31 09:23:55
报纸
[Java] 纯文本查看 复制代码
        String str = "goOd gooD stUdy dAy dAy up";

        Matcher m = Pattern.compile("(^| )([a-z])([a-zA-Z]*)").matcher(str);
        StringBuffer sb = new StringBuffer();
        while (m.find()) {
           m.appendReplacement(sb, "$1" + m.group(2).toUpperCase() + "$3");
        }
        
        System.out.println(sb);

输出:
[Java] 纯文本查看 复制代码
GoOd GooD StUdy DAy DAy Up
回复 使用道具 举报
package com.heima.practice;

/** 6.将"goOd gooD stUdy dAy dAy up"
* 每个单词的首字母转换成大写其余还是小写字母(不许直接输出good good study day day up 要用代码实现)*/
public class Test3 {
        public static void main(String[] args) {
        String s = "goOd gooD stUdy dAy dAy up";
        String s1 =s.toLowerCase();
        String[] arr = s1.split(" ");
        String s6 ="";
        for (String string : arr) {
                String s2=string.substring(0, 1);
                String s3=string.substring(1);
               
                String s4 = s2.toUpperCase();
                String s5 = s4.concat(s3);
                 s6= s6.concat(s5.concat(" "));
               
        }
        System.out.println(s6);

方法比较麻烦,自己打的
回复 使用道具 举报
zl123 初级黑马 2016-7-31 10:09:33
7#
package com.heima.practice;

/** 6.将"goOd gooD stUdy dAy dAy up"
* 每个单词的首字母转换成大写其余还是小写字母(不许直接输出good good study day day up 要用代码实现)*/
public class Test3 {
        public static void main(String[] args) {
        String s = "goOd gooD stUdy dAy dAy up";
        String s1 =s.toLowerCase();
        String[] arr = s1.split(" ");
        String s6 ="";
        for (String string : arr) {
                String s2=string.substring(0, 1);
                String s3=string.substring(1);
               
                String s4 = s2.toUpperCase();
                String s5 = s4.concat(s3);
                 s6= s6.concat(s5.concat(" "));
               
        }
        System.out.println(s6);


自己打的,方法可能比较麻烦
回复 使用道具 举报
qetu001212 发表于 2016-7-29 23:58
String s= "goOd gooD stUdy dAy dAy up";
s= s.toLowerCase();
s=s.insert(0,"G");

确实能实现,但是感觉代码复用性太差
回复 使用道具 举报
zl123 发表于 2016-7-31 10:07
package com.heima.practice;

/** 6.将"goOd gooD stUdy dAy dAy up"

这是我写的,你看看

/*
* 6.将"goOd gooD stUdy dAy dAy up"
* 每个单词的首字母转换成大写其余还是小写字母(不许直接输出good good study day day up 要用代码实现)
*/
public class Sixth_BigtoSmall {
        public static void main(String[] args) {
                String str = "goOd gooD stUdy dAy dAy up";
                String[] str2 = str.trim().split(" ");
                for (int i = 0; i < str2.length; i++) {
                        char ch = Character.toUpperCase(str2.charAt(0));
                        String str3 = str2.substring(1).toLowerCase();
                        System.out.print((ch + str3) + " ");
                }
               
        }
}

回复 使用道具 举报
zl123 来自手机 初级黑马 2016-8-2 21:34:40
10#
提醒到我了,字符串可以看成对象,链式编程。
回复 使用道具 举报
zl123 发表于 2016-8-2 21:34
提醒到我了,字符串可以看成对象,链式编程。

链式编程在实际开发中灰常常用
回复 使用道具 举报
刘文秀 发表于 2016-8-1 00:10
这是我写的,你看看

/*

可以的!!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马