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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© YI_LI_A_E 中级黑马   /  2015-7-18 21:29  /  3069 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

3黑马币
  1. /*
  2. 需求:获取一个字符串在另一个字符串中出现的次数例如"abkkcdkkefkkskk"
  3. 思路:1、定义一计数器
  4.           2、获得字符串第一次在另个字符中出现的位置
  5.           3、从第一次出现的位置后剩余的字符串中继续获取字符串出现的次数,每出现一次,计数器就加一次
  6.           3、当获取不到时候,计数停止
  7. */
  8.         class StringTestDemo
  9. {
  10.         public static void main(String[] args)
  11.         {
  12.                 method_getCount();
  13.         }
  14.         public static void method_getCount()
  15.         {
  16.                 String s1="abkkcdkkefkkskk";
  17.                 System.out.println(getCount(s1,"kk"));
  18.         }
  19.         public static int getCount(String str,String key)
  20.         {
  21.                 int count=0;
  22.                 int index=0;
  23.                 while((index=str.indexOf(key))!=-1)
  24.                 {        
  25.                         str.substring(index+key.length());
  26.                         count++;
  27.                         System.out.println(index);
  28.                 }
  29.                 return count;
  30.         }
  31. }
复制代码
还有下面的程序字符串两边去空格的,是不是因为substring的原因
  1. class StringTestDemo
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 method_myTrim();
  6.                 method_reverse();
  7. //                method_getCount();
  8.                 method_getCount2();
  9.         }
  10. /************************************************************************************************/
  11.         public static void method_myTrim()//自己定义函数实现去除字符串两边的空格
  12.         {
  13.                 String s1=" abc ";
  14.                 System.out.println(s1);
  15.                 System.out.println("("+myTrim(s1)+")");        
  16.         }
  17.         public static String myTrim(String str)
  18.         {
  19.                 int start=0,end=str.length()-1;
  20.                 while(start<=end && str.charAt(start)==' ');
  21.                 start++;
  22.                 while(start<=end && str.charAt(end)==' ');
  23.                 end--;
  24.                 return str.substring(start,end+1);
  25.         }
  26. }
复制代码



最佳答案

查看完整内容

第一个是因为str.substring()方法的作用是返回一个新的子字符串,而不是把字符串str切割,所以str是一直没变的,所以成了死循环,要用str接收返回的子字符串str=str.substring(index + key.length()); 第二个应该是你的失误,你的两个while循环后面多了分号;,单独的一个分号相当于一个空语句,你按下Ctrl+Shift+F格式化代码就能看出来,while不带大括号时默认循环体是后面的一句语句。空语句也是语句,所以你的循环执行的就是 ...

12 个回复

倒序浏览
本帖最后由 a12366456 于 2015-7-19 00:19 编辑

第一个是因为str.substring()方法的作用是返回一个新的子字符串,而不是把字符串str切割,所以str是一直没变的,所以成了死循环,要用str接收返回的子字符串str=str.substring(index + key.length());
  1. public static int getCount(String str, String key) {
  2.                 int count = 0;
  3.                 int index = 0;
  4.                 while ((index = str.indexOf(key)) != -1) {
  5.                         str=str.substring(index + key.length());
  6.                         count++;
  7.                         //System.out.println(index);
  8.                 }
  9.                 return count;
  10.         }
复制代码

第二个应该是你的失误,你的两个while循环后面多了分号;,单独的一个分号相当于一个空语句,你按下Ctrl+Shift+F格式化代码就能看出来,while不带大括号时默认循环体是后面的一句语句。空语句也是语句,所以你的循环执行的就是那个空语句,去掉那两个分号即可
回复 使用道具 举报
public static int getCount(String str,String key)

20.        {

21.                int count=0;

22.                int index=0;

23.                while((index=str.indexOf(key))!=-1)

24.                {        

25.                        str.substring(index+key.length());你只是取出了剩余的字符串,没有改变字符串啊,应该是 str=str.substring(index+key.length());

26.                        count++;

27.                        System.out.println(index);输出index没有意义了,因为每找到一次,str都会改变,已不是最初的str了

28.                }

29.                return count;

30.        }

点评

听说评论可以增加黑马币,试试  发表于 2015-7-18 22:54
回复 使用道具 举报
public static String myTrim(String str)

18.        {

19.                int start=0,end=str.length()-1;

20.                while(start<=end && str.charAt(start)==' ');多写了一个;

21.                start++;

22.                while(start<=end && str.charAt(end)==' ');多写了一个;

23.                end--;

24.                return str.substring(start,end+1);

25.        }
回复 使用道具 举报
刚上了三天基础班的菜鸟表示看不懂
回复 使用道具 举报
你都没有创建一个新的字符串,每次你都是在判断原来的那个字符串str的那个“kk”的第一次出现的值是否不小于-1,那是当然的啦,所以就会一直的无限循环下去,解决的办法就是将你截取的字符串赋值给原来的那个字符串参与后面的运算就OK了
回复 使用道具 举报
public static int getCount(String str,String key)
        {
                int count=0;
                while(str.indexOf(key)!=-1)
                {        int index = str.indexOf(key);
                        str = str.substring(index+key.length());
                        count++;
                       
                }
                return count;
        }
这样写就对了
回复 使用道具 举报
利用substring()方法对字符串进行截取的时候,如果程序发现截取到的字符串两边有空字符串,就会自动调用trim的方法,将两端的空字符串删除,但是注意,这是删除两边的空字符串,中间的是不删除的
回复 使用道具 举报
看回答也能学到很多知识啊
回复 使用道具 举报
看的瞌睡了
回复 使用道具 举报
libin 中级黑马 2015-7-21 17:50:38
11#
果然是高手如云啊。你这个思路都没有错,就是不太细心,也是练习得比较少,多敲敲代码就好了。
回复 使用道具 举报
多敲敲代码就会了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马