黑马程序员技术交流社区

标题: 请问下边代码中startWith和charAt的区别 [打印本页]

作者: Cola    时间: 2013-12-2 13:13
标题: 请问下边代码中startWith和charAt的区别
本帖最后由 Cola 于 2013-12-3 07:35 编辑

去除字符串两端空格,现在while中用startsWith和endsWith判断,没有出现想要的结,但是更改下while判断语句,其他都不改。while(start<=end && str.charAt(start)==' ')       while(start<=end && str.charAt(end)==' ')    ,这样就可以。请问为什么呢?
  1. class StringTest1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String s="      ab  cd     ";
  6.                 //sop(s);
  7.                 String s1=myTrim1(s);
  8.                 sop(s1);

  9.         }
  10.         public static void sop(Object obj)
  11.         {
  12.                 System.out.println(obj);
  13.         }

  14.         public static String myTrim1(String str)
  15.         {
  16.                 int start=0;
  17.                 int end=str.length()-1;
  18.                 while(start<=end&&str.startsWith(" "))
  19.                 {
  20.                         start++;
  21.                 }
  22.                 while(start<=end&&str.endsWith(" "))
  23.                 {
  24.                         end--;
  25.                 }
  26.                 sop(start);
  27.                 sop(end);
  28.                 str=str.substring(start,end+1);
  29.                 return str;
  30.         }

  31. }
复制代码




作者: 发哥-阿花    时间: 2013-12-2 15:09
本帖最后由 发哥-阿花 于 2013-12-2 16:04 编辑

你的代码没有改动时,是因为每次都是在判断str的第一个字符,然而循环完都满足。要想得到想要的结果,每次判断都要使其判断下一个字符,就和你改动过的一个道理。我这样说能看得懂吗?
  1. class StringTest1
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 String s="      ab  cd     ";
  6.                 //sop(s);
  7.                 String s1=myTrim1(s);
  8.                 sop(s1);

  9.         }
  10.         public static void sop(Object obj)
  11.         {
  12.                 System.out.println(obj);
  13.         }

  14.         public static String myTrim1(String str)
  15.         {
  16.                 int start=0;
  17.                 int end=str.length()-1;
  18.                 while( str.startsWith(" ") && start<=end)
  19.                  //这里循环判断str的第一个字符,直到判断完字符串,结束,
  20.                 {
  21.                         start++;//循环结束后start的值是17
  22.                         //你要改变原字符串再判断,
  23.                 }
  24.                 while(start<=end && str.endsWith(" "))
  25.                 {
  26.                         end--;//与上面一个意思
  27.                 }
  28.                 str=str.substring(start,end+1);
  29.                 return str;
  30.         }

  31. }
复制代码
下面是改了的代码,

  1. public static String myTrim1(String str)
  2.         {
  3.                 int start=0;
  4.                 while( str.startsWith(" ") && start<=str.length()-1)
  5.                 {
  6.                        // start++;
  7.                        str = str.substring(start+1);//每循环一次就减去前面一个空格
  8.                 }
  9.                int end = str.length()-1;//上个循环之后再定义end
  10.                 while(str.endsWith(" ") && end>=0)
  11.                 {
  12.                         end--;
  13.                         str = str.substring(0,end);//循环时减去最后面的空格
  14.                 }
  15.                 //str=str.substring(start,end);//这步不用做了,循环之后就是结果,return就可以了
  16.                 return str;
  17.         }

复制代码






作者: Cola    时间: 2013-12-3 07:34
发哥-阿花 发表于 2013-12-2 15:09
你的代码没有改动时,是因为每次都是在判断str的第一个字符,然而循环完都满足。要想得到想要的结果,每次 ...

明白啦,谢谢啦




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