黑马程序员技术交流社区

标题: 帮忙解答 [打印本页]

作者: 某某帅    时间: 2016-9-9 21:54
标题: 帮忙解答
怎么把字符串helloworld中的h和w换成大写,最后输出HelloWorld
作者: kangnam    时间: 2016-9-9 22:13
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);
        }
作者: a869588615    时间: 2016-9-9 22:13
使用StringBuffer类的replacement将h和w直接替换成想要的字符
作者: kangnam    时间: 2016-9-9 22:29
                String s ="helloworld";
                String s2 =s.replace('h', 'H');
                String s3=s2.replace('w', 'W');
                System.out.println(s3);
作者: 流空有痕    时间: 2016-9-10 00:33
  String s ="helloworld";
                String s2 =s.replace('h', 'H');
                String s3=s2.replace('w', 'W');
                System.out.println(s3);
作者: lipeng518    时间: 2016-9-10 00:36
StringBuffer类有个replacement的方法可以替换




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2