黑马程序员技术交流社区

标题: 把this is a text转换成This Is A Text [打印本页]

作者: 钱金磊    时间: 2016-5-1 00:20
标题: 把this is a text转换成This Is A Text
  1. /*this is a text  This  Is  A Text */
  2.                 String s="this is a text";
  3.                 String s1="";
  4.                 int i=0;
  5.                 int i2=0;
  6.                
  7.                         while(true){
  8.                                 s1=s1+s.toUpperCase().substring(i,i+1);                        //截取第一个字符并转换为大写字母
  9.                                 if(s.indexOf(" ",i2+1)==-1){                                        //假设我在空格后没有空格了,我就直接截取到尾部
  10.                                         s1=s1+s.substring(i+1);
  11.                                         i=i2;
  12.                                         break;                                                                                //结束后我就挑掉
  13.                                 }else{
  14.                                         i2=s.indexOf(" ",i);                                                //假设还有空格,记录下一个索引位置
  15.                                         s1=s1+s.substring(i+1,i2)+" ";//                        //我就直接截取到空格位置
  16.                                         i=i2+1;                                                                                //我把截取的地址+1就是下个单词的首地址
  17.                                 }
  18.                        
  19.                         }
  20.                         System.out.println(s1);
复制代码
不能用srtingbuffer;因为stringbuffer不支持字符串大小写转换!




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