查找字符串中的子字符串数目

public int getSubCount(String Content, String Sub)
{
  int Count=0;
  if(Sub.length()!=0){
  Count = Content.length();
  Content = Content.replaceAll(Sub,"");
  Count = Count - Content.length();
  Count = Count / Sub.length();
  }
  ruturn Count;
}
---------------------------------------------------------------

public int getSubCount(String Content, String Sub)
{
   return Content.split(Sub).length-1 ;
}
---------------------------------------------------------------

Matcher m=Pattern.compile(sub).match(content);
写错了,后面方法应该是matcher
应该是:
Matcher m=Pattern.compile(sub).matcher(content);
或者用
Matcher m=Pattern.matches(sub,content);
效果是一样的。


---------------------------------------------------------------

/*
 * Created on 2006-6-7
 *
 * 
 */
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTest3 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        final int n = 10000000;
        
        StringBuffer sb = new StringBuffer(n);
        for(int i=0;i<(n/10);i++){
            sb.append("abcdefghij");            
        }
        
        String s = sb.toString();
        String q = "j";  //子串
        
        
        long t1,t2;
        t1 =  System.currentTimeMillis();
        int l1 = test1(s, q);
        t2 = System.currentTimeMillis();
        System.out.println("1 find count:" + l1 + "/ttime:" + (t2 - t1));
        
        
        t1 =  System.currentTimeMillis();
        int l2 = test2(s, q);
        t2 = System.currentTimeMillis();
        System.out.println("2 find count:" + l2 + "/ttime:" + (t2 - t1));
        
        t1 =  System.currentTimeMillis();
        int l3 = test3(s, q);
        t2 = System.currentTimeMillis();
        System.out.println("3 find count:" + l3 + "/ttime:" + (t2 - t1));
        
        t1 =  System.currentTimeMillis();
        int l4 = test3(s, q);
        t2 = System.currentTimeMillis();
        System.out.println("4 find count:" + l4 + "/ttime:" + (t2 - t1));
    }
    
    public static int test1(String content, String sub){
        int count = 0;
      
        int p = content.indexOf(sub.charAt(0));
        do{
            next:
            if(p > -1){ //查找第一个字符,如果相等则继续比较第二位
                if(p <= (content.length() - sub.length()))
                {
                    for(int j=1;j<sub.length();j++){
                        if(content.charAt(p + j) != sub.charAt(j)){
                            break next;
                        }
                    }
                    count ++;
                }
                else{
                    break;
                }
            }
        
            p = content.indexOf(sub.charAt(0), p + 1);
        }
        while(p > -1);

        return count;
    }

    public static int test2(String content, String sub){
        int count = 0;
        if (sub.length() != 0) {
            count =content.length();
            content = content.replaceAll(sub, "");
            count = count - content.length();
            count = count / sub.length();
        }
        return count;
    }
 
    
    public static int test3(String Content, String Sub)
    {
       return Content.split(Sub).length-1 ;
    }
    
    public static int test4(String content, String sub)
    {
      int count=0;
      Matcher m=Pattern.compile(sub).matcher(content);
      while(m.find()){
        count++;
      }
      return count;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值