zoukankan      html  css  js  c++  java
  • 统计一段长字符串中某字符串的出现次数

    • 截取字符串统计字符串出现次数
    • 通过替换字符串,统计字符串出现次数
    • 通过正则表达式,统计字符串出现次数
    package constxiong.interview;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * 统计一段长字符串中某字符串的出现次数
     * @author ConstXiong
     * @date 2019-11-13 11:01:22
     */
    public class TestCountWordTimesInText {
    
        public static void main(String[] args) {
            String text = "统计一CX段长CX字符串中某字符串的出C现X次cx数";
            String word = "CX";
            System.out.println(countWordTimesByCutString(text, word));
            System.out.println(countWordTimesByReplace(text, word));
            System.out.println(countWordTimesByRegex(text, word));//正则匹配,需要注通配符的使用对结果的影响
        }
    
        /**
         * 截取字符串统计字符串出现次数
         * @param text
         * @param word
         * @return
         */
        public static int countWordTimesByCutString(String text, String word) {
            int times = 0;
            if (!isEmpty(text) && !isEmpty(word)) {
                String subText = text;
                int index = -1;
                int wordLength = word.length();
                while (subText.length() >= wordLength && (index = subText.indexOf(word)) > -1) {
                    subText = subText.substring(index + wordLength);
                    times++;
                }
            }
            return times;
        }
        
        /**
         * 通过替换字符串,统计字符串出现次数
         * @param text
         * @param word
         * @return
         */
        public static int countWordTimesByReplace(String text, String word) {
            int times = 0;
            if (!isEmpty(text) && !isEmpty(word)) {
                times = (text.length() - text.replace(word, "").length()) / word.length();
            }
            return times;
        }
        
        /**
         * 通过正则表达式,统计字符串出现次数
         * @param text
         * @param word
         * @return
         */
        public static int countWordTimesByRegex(String text, String word) {
            int times = 0;
            if (!isEmpty(text) && !isEmpty(word)) {
                Pattern p = Pattern.compile(word);
                Matcher m = p.matcher(text);
                while (m.find()) {
                    times++;
                }
            }
            return times;
        }
        
        /**
         * 字符串是否为空
         * @param str
         * @return
         */
        private static boolean isEmpty(String str) {
            return str == null || str.length() == 0;
        }
        
    }


    原文链接
     


     

  • 相关阅读:
    生成随机端口函数
    于获得MFC窗口其它类指针的方法
    VC6.0中使用ADO操作Access数据库 (转)
    【原创】C++利用IXMLDOM解析XML文件。
    转帖:用MFC对话框做无闪烁图片重绘一一 程序设计: icemen
    C代码优化方案(转)
    【转】C++ Socket UDP "Hello World!"
    线程中使用UpdateData出错解决方法(转)
    C语言调试打印log函数。
    Windows Sockets 网络编程(三) —— WINDOWS SOCKETS 1.1 程序设计(转)
  • 原文地址:https://www.cnblogs.com/ConstXiong/p/12164934.html
Copyright © 2011-2022 走看看