zoukankan      html  css  js  c++  java
  • 查找字符串中的子字符串数目

    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;
        }
    }

  • 相关阅读:
    安卓小助手
    位图切割器&位图裁剪器
    OAuth2.0 的简介
    多账户的统一登录方案
    常用的一些SQL语句
    SQL语句的优化
    SpringCloud简介与5大常用组件
    数据库为什么需要锁机制?有哪些锁机制?
    高并发下如何优化能避免服务器压力过大?
    Spring MVC 简介
  • 原文地址:https://www.cnblogs.com/tongdengquan/p/6090593.html
Copyright © 2011-2022 走看看