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

© Cola 中级黑马   /  2013-12-2 13:13  /  1274 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 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. }
复制代码



评分

参与人数 1技术分 +1 黑马币 +6 收起 理由
枫儿 + 1 + 6 神马都是浮云

查看全部评分

2 个回复

倒序浏览
本帖最后由 发哥-阿花 于 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.         }

复制代码





评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1 很给力!

查看全部评分

回复 使用道具 举报
发哥-阿花 发表于 2013-12-2 15:09
你的代码没有改动时,是因为每次都是在判断str的第一个字符,然而循环完都满足。要想得到想要的结果,每次 ...

明白啦,谢谢啦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马