黑马程序员技术交流社区

标题: 获取一段字符串在指定的字符串中出现的次数 [打印本页]

作者: lockwood    时间: 2015-5-13 23:49
标题: 获取一段字符串在指定的字符串中出现的次数
public class StringTest1 {
        public static void main(String[] args) {
                System.out.println("获取一段字符串在指定的字符串中出现的次数");
                String yoursecret = "fsdkfhsecretsdfsecretsndsecretshedfhsecret";
                String secret = "secret";
                System.out.println(count2(secret, yoursecret));
               
               
               
        }
        public static int count2(String smallstring, String bigstring) {
                int count = 0;
                int i = bigstring.indexOf(smallstring);
                while(i != -1){
                        bigstring = bigstring.substring(i + smallstring.length());
                        count++;
                        i = bigstring.indexOf(smallstring);
                }
                return count;
               
        }
}




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