黑马程序员技术交流社区

标题: String 类的方法调用 [打印本页]

作者: huan111    时间: 2016-5-25 00:02
标题: String 类的方法调用
.将"hello world"字符串中每个单词的首字母转换成大写其余字母还是小写(不能直接输出 Hello World而是用代码实现)????


作者: 18735346124    时间: 2016-5-25 00:40
public class Test {
       public static void main(String[] args) {
             String s = "hello world";
             String[] arr = s.split(" ");
             StringBuffer sb = new StringBuffer();
             for (int i = 0; i < arr.length; i++) {
        sb.append(arr[i].substring(0, 1).toUpperCase().concat(arr[i].substring(1).toLowerCase()));
             }
                System.out.println(sb);
        }
}

作者: huan111    时间: 2016-5-25 00:52
18735346124 发表于 2016-5-25 00:40
public class Test {
       public static void main(String[] args) {
             String s = "hello w ...

谢谢,.append( " " )再在中间加一个空格
作者: z736886202    时间: 2016-5-25 00:57
瞧着熟练的手法 ......顶一个
作者: 驰马定中原    时间: 2016-5-25 01:54
public class 题目 {
       public static void main(String[] args) {
               String s = "hello world";
               String s1 =s.replace('h', 'H');
               String s2 =s1.replace('w', 'W');
              
               System.out.println(s2);
       }   
}
作者: 驰马定中原    时间: 2016-5-25 01:55
驰马定中原 发表于 2016-5-25 01:54
public class 题目 {
       public static void main(String[] args) {
               String s = "hello world" ...

这样可以吧
作者: 驰马定中原    时间: 2016-5-25 02:02
驰马定中原 发表于 2016-5-25 01:54
public class 题目 {
       public static void main(String[] args) {
               String s = "hello world" ...

这样可以吧
作者: 驰马定中原    时间: 2016-5-25 02:20
public class 题目 {
       public static void main(String[] args) {
               String s = "hello world";
               char[] cha = s.toCharArray();
               StringBuffer sb = new StringBuffer();
               for (int i = 0; i < cha.length; i++) {
                       if (cha[i]=='h') {
                                cha[i]='H';
                        }
                       if (cha[i]=='w') {
                                cha[i]='W';
                        }
                       sb.append(cha[i]);
                }
               System.out.println(sb);
作者: liucuifu    时间: 2016-5-25 08:14
好简单.........
作者: ancheng    时间: 2016-5-25 08:55
  1. String str = "hello world";
  2.                 System.out.println(str.substring(0,1).toUpperCase()+str.substring(1,6)+str.substring(6,7).toUpperCase()+str.substring(7));
复制代码





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