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

© 某某帅 中级黑马   /  2016-9-9 21:54  /  397 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

怎么把字符串helloworld中的h和w换成大写,最后输出HelloWorld

5 个回复

倒序浏览
public static void main(String[] args) {
                String s ="helloworld";
                char[] s1 =s.toCharArray();
                for(int i=0;i<s1.length;i++){
                        if(s1[i]=='h'){
                                s1[i]='H';
                        }else if(s1[i]=='w'){
                                s1[i]='W';
                        }
                }
                String s2 =new String().valueOf(s1);
                System.out.println(s2);
        }
回复 使用道具 举报
使用StringBuffer类的replacement将h和w直接替换成想要的字符
回复 使用道具 举报
                String s ="helloworld";
                String s2 =s.replace('h', 'H');
                String s3=s2.replace('w', 'W');
                System.out.println(s3);
回复 使用道具 举报
  String s ="helloworld";
                String s2 =s.replace('h', 'H');
                String s3=s2.replace('w', 'W');
                System.out.println(s3);
回复 使用道具 举报
StringBuffer类有个replacement的方法可以替换
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马