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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lnz_黑仔 中级黑马   /  2014-12-31 01:08  /  1056 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package cn.itcast12_03;

/*
* 获取一个字符串中指定子串出的次数。比如说“fsdhanbasdnbafllgnbahjnbakqqqqlnbaxi” 在这个字符串中,多有个nba?
*/
public class StringTest1 {
        public static void main(String[] args) {
                String str = "fsdhanbasdnbafllgnbahjnbakqqqqlnbaxnbai";
                String regex = "nba";
                // 功能
                int count = getCount(str, regex);
                int count2 = getCount2(str, regex);
                System.out.println(count);
        }

        /*
         * 返回值:统计变量的值 int 参数列表:大串和小串
         */
        public static int getCount(String maxString, String minString) {
                // 定义统计变量
                int count = 0;
                // 在大串中查找小串一次
                int index = maxString.indexOf(minString);
                // 如果返回值不是-1,说明小串在大串中是存在的。
                while (index != -1) {
                        // 统计变量++
                        count++;
                        // 把查找过的数据给截取掉,重新赋值给大串
                        maxString = maxString.substring(index + minString.length());
                        // 在新的大串中查找一次小串
                        index = maxString.indexOf(minString);
                }
                return count;
        }

        // 优化代码
        //
        public static int getCount2(String maxString, String minString) {
                // 定义统计变量
                int count = 0;
                // 在大串中查找小串一次
                int index = 0;
                // 如果返回值不是-1,说明小串在大串中是存在的。
                // 判断
                while ((index  = maxString.indexOf(minString)) != -1) {
                        // 统计变量++
                        count++;
                        // 把查找过的数据给截取掉,重新赋值给大串
                        maxString = maxString.substring(index + minString.length());
                }
                return count;
        }
}



/*String s1="a"+"b"+"c";
                String s2="abc";
                System.out.println(s1==s2);//true  ???????????
                System.out.println(s1.equals(s2));// true       
                String s1="ab";
                String s2="abc";
                String s3=s1+"c";
                System.out.println(s3==s2);//false  ???????????
                System.out.println(s3.equals(s2));// true*/


5 个回复

倒序浏览
String s1="a"+"b"+"c";//使用双引号创建字符串与用new完全不同,他会检测在栈中的字符串存储池中是否有值为arit的字符串,如果有则指向它,如果没有,则在栈中创建它;
String s2="abc";//上一句已经在栈中的字符串存储池中有值为abc的字符串,s2会指向它;
System.out.println(s1==s2);//s1和s2指向同一个为abc的字符串,故为true;
String s1="ab";//如上所说,在存储池中创建一个ab的字符串
String s2="abc";
String s3=s1+"c";//由于字符串是不可变的,所以这里的s1+"c"并不是s3由s1+c而变成abc,而是在堆中创建了一个新的值为abc的字符串,注意这里是在堆中创建的,而没有在栈中的字符串存储池中寻找arit然后共享,然后让s3指向了它。
System.out.println(s3==s2);//理解了前面就理解了这个,虽然s2的值与s3一样都为arit,但是却没有实现共享机制,因为s3不是用s3 = “abc”的形式创建的,s3创建在堆中,而s2是创建在了共享池中。二者指向不同的对象,所以用==比较为false。
回复 使用道具 举报
学习学习!!!
回复 使用道具 举报
楼主给力
回复 使用道具 举报
个人认为直接利用String中IndexOf(String substr,int FromIndex)方法来实现可能会比较简单。代码如下:
  1. import java.lang.*;
  2. public class StringCounterDemo {
  3.        
  4.         static int  SubStringCount(String str,String substr)
  5.         {
  6.                 int count = 0,fromindex=0,k=0;
  7.                 int sub_len = substr.length();
  8.                 while(fromindex < str.length()){
  9.                     k=str.indexOf(substr, fromindex);
  10.                     if(k==-1)
  11.                     {
  12.                             break;
  13.                     }
  14.                     else
  15.                     {
  16.                             count ++;
  17.                             fromindex = k + sub_len;
  18.                     }
  19.                 }
  20.                 return count;
  21.         }

  22.         public static void main(String[] args) {
  23.                 // TODO 自动生成的方法存根
  24.        System.out.println("SubCount = " +SubStringCount("abc","abcdef") );
  25.        System.out.println("SubCount = " +SubStringCount("abcde","gf") );
  26.        System.out.println("SubCount = " +SubStringCount("abcdeabab","ab") );
  27.         }
  28. }
复制代码



回复 使用道具 举报
好厉害。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马