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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 梁志兵 中级黑马   /  2013-4-5 21:50  /  1576 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class StrigTest3
{
public static void main(String[] args)
{
int count =getCount("afrbcfrtyhgtfrbgfrd","fr");
   
System.out.println("count="+count);
}
    public static int getCount(String str,String sub)
    {
  int count=0;
  int index=0;
  while ((index = str.indexOf(sub))!=-1)//对于这个while语句的作用不是很明白,请高手指点
  {
          str = str.substring(index+sub.length(),str.length());
    count++;
  }
  return count;
    }
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

13 个回复

倒序浏览
while ((index = str.indexOf(sub))!=-1)

指定字符,查找字符在字符串中的下标,找不到就是-1
这是一个循环  健壮性判断  也就是当 还有 "fr"字符串的 时候  下面的 count++, 否则就是没有了查完了.

你主要不懂的是!=-1这个概念  还有你需要明白的是 ((index = str.indexOf(sub))!=-1)  的意思.
回复 使用道具 举报
把需求写出来嘛!
回复 使用道具 举报
class StrigTest3
{
public static void main(String[] args)
{
int count =getCount("afrbcfrtyhgtfrbgfrd","fr");
   
System.out.println("count="+count);
}
    public static int getCount(String str,String sub)
    {
  int count=0;
  int index=0;
  while ((index = str.indexOf(sub))!=-1)//对于这个while语句的作用不是很明白,请高手指点
  {
          str = str.substring(index+sub.length(),str.length());
    count++;
  }
  return count;
    }
}

把代码(index = str.indexOf(sub))!=-1拿出来
(index = str.indexOf(sub))!=-1
index = str.indexOf(sub)
str.indexOf(sub)

其中str.indexOf(sub)是判断sub在str中是否存在 如果存在就返回大于或等于零的数,如果不存在就返回-1 给index
如果不是-1就进入循环
str = str.substring(index+sub.length(),str.length());是从找到的字符串向后截取并且再次付给str(因为下次循环还要比较)
count是记录字符串出现的次数。

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
String 的index(String str)方法的功能是:返回指定子字符串在此字符串中第一次出现处的索引;如果字符串参数作为一个子字符串在此对象中出现,则返回第一个这种子字符串的首字符的索引;如果它不作为一个子字符串出现,则返回 -1。
因此 (index = str.indexOf(sub))!=-1  就是循环查找sub在str中是否出现,直到查找不到。str根据返回的索引,通过str = str.substring(index+sub.length(),str.length());语句截取查找完sub后的剩余部分

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
while ((index = str.indexOf(sub))!=-1)
从str中查找sub字串,找到返回查找字符在字符串中的下标,找不到就返回-1;
就是说有“fr”字串时,进入循环,count++;找到最后没了就返回-1,循环就结束了

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
lucy198921 发表于 2013-4-5 22:02
while ((index = str.indexOf(sub))!=-1)

指定字符,查找字符在字符串中的下标,找不到就是-1

((index = str.indexOf(sub))!=-1)  的意思.有点不明白?
回复 使用道具 举报
梁志兵 发表于 2013-4-5 22:48
((index = str.indexOf(sub))!=-1)  的意思.有点不明白?

str.indexOf(sub)是判断sub在str中是否存在 如果存在就返回大于或等于零的数,如果不存在就返回-1 给index
如果不是-1就进入循环
str = str.substring(index+sub.length(),str.length());是从找到的字符串后截取并再次赋给str
回复 使用道具 举报
柳 德 彬 发表于 2013-4-5 22:04
把需求写出来嘛!

((index = str.indexOf(sub))!=-1)  的意思.有点不明白?
回复 使用道具 举报
老师那样写确实不怎好看,这是我写的,应该比老师写的容易看些
public class StringTest {
        static int count = 0;

        public static void main(String[] args) {

                String s = "sdkkffkksfskkdfskkdfsdkk";
                String s1 = "kk";
                getZiString(s, s1);
                System.out.println(count);
        }

        public static int getZiString(String str1, String str2) {
                int index = 0;
                while ((index = str1.indexOf(str2)) != -1) {
                        str1 = str1.substring(index + str2.length(), str1.length());
                        count++;
                }
                return count;
        }
}

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
问题帮你改成【已解决】了~  回答的很详细了~  下回有不懂的方法,可以查API~
回复 使用道具 举报
陈丽莉 发表于 2013-4-6 00:35
问题帮你改成【已解决】了~  回答的很详细了~  下回有不懂的方法,可以查API~ ...

谢谢 那个不怎么会看:loveliness:
回复 使用道具 举报
HM何伟 发表于 2013-4-5 23:03
老师那样写确实不怎好看,这是我写的,应该比老师写的容易看些
public class StringTest {
        static int count ...

嗯 这样是好看点
回复 使用道具 举报
lucy198921 发表于 2013-4-5 22:53
str.indexOf(sub)是判断sub在str中是否存在 如果存在就返回大于或等于零的数,如果不存在就返回-1 给inde ...

谢谢 指教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马