获取指定字符串在另一个字符串中出现的次数

方法一

public int count(String srcText, String findText) {
        String[] array = srcText.split(findText);
        return array.length - 1;
}  

方法二

public static int count(String srcText, String findText) {
    int count = 0;
    Pattern p = Pattern.compile(findText);
    Matcher m = p.matcher(srcText);
    while (m.find()) {
        count++;
    }
    return count;
}  

方法三

public static int count(String srcText, String findText) {
    int count = 0;
    int index = 0;
    while ((index = srcText.indexOf(findText, index)) != -1) {
        index = index + findText.length();
        count++;
    }
    return count;
}
时间: 2024-08-26 12:52:30

获取指定字符串在另一个字符串中出现的次数的相关文章

Java实现统计某字符串在另一个字符串中出现的次数

面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 下面是实现代码: /** * 统计某字符串在另一个字符串中出现的次数 * * */ public class CountHit { public static void main(String[] args) { String a = "123456abcde6ab"; String b = "6abc"

统计某字符串在另一个字符串中出现的次数

1 /** 2 * 统计某字符串在另一个字符串中出现的次数 3 * 4 * 5 */ 6 public class CountHit { 7 public static void main(String[] args) { 8 String a = "123456abcdde6abcbcb"; 9 String b = "6abc"; 10 System.out.println(new CountHit().hit(a, b)); 11 } 12 13 /** 14

在php中判断一个字符串包含另一个字符串

方法一:用php的strpos() 函数判断字符串中是否包含某字符串的方法 if(strpos('Longway','way') !== false){ echo '包含way'; }else{ echo '不包含way'; } 方法二:使用了explode 用explode进行判断PHP判断字符串的包含代码如下: <?php $url = "001a.gif"; $str = "a"; $con = explode($str,$url); if (count

获取指定元素的某一个样式属性值

1."元素.style.样式属性名" ->oDiv.style.height 弊端:"只能"获取在"行内"上编写过的样式,不管在哪些了对应的样式,只要没有在行内上写过,都获取不到 oDiv.style.height 由于height并没有写在行内样式上(写在样式表里了),所以获取的结果依然是"" 2.通过 window.getComputedStyle 获取所有经过浏览器计算的样式(只要的你的元素在浏览器加载的时候渲染过

python练习:编写一个函数isIn,接受两个字符串作为参数,如果一个字符串是另一个字符串的一部分,返回True,否则返回False。

重难点:定义函数的方法.使用str类型的find()函数,可以查找多个字符.第二种方法为把字符串转化为字符队列,然后遍历寻找,但是只可以寻找一个字符. 1 print("----------------------------") 2 def isIn(x,y):#def定义函数保留字 3 v=y.find(x) 4 if v>=0: 5 return True; 6 else: 7 return False; 8 print(isIn('sxc','azdsxcv'))#输出函

输入5个学生的名字(英文),使用冒泡排序按从大到小排序。 提示:涉及到字符串数组,一个字符串是一个一维字符数组;一个 字符串数组就是一个二维字符数组。

#include <stdio.h>#include <string.h> /*输入5个学生的名字(英文),使用冒泡排序按从大到小排序.提示:涉及到字符串数组,一个字符串是一个一维字符数组:一个字符串数组就是一个二维字符数组.*/ void main(){ char stuNames[5][100]; char tmp[100]; int i = 0,j = 0; for(i = 0; i < 5; i++) { printf("请输入第%d个学生的名字:"

在一个字符串寻找另一个字符串,并且输出短字符串头字母在长字符串的下标

录入两个字符串,一个字符串是另一个字符串的字串,输出子串首字母在长字符串的位置. 两个解决办法 使用库函数strncmp(str1,str2,n); 功能:比较str1和str2两个字符串的前n个字母,一一比较,若str1大则返回1,若str1和str2相等,则返回0,否则返回-1: int compare(char *str1, char *str2) { int i; int lenstr1 = strlen(str1); int lenstr2 = strlen(str2); if( le

Java 一个字符串在另外一个字符串出现次数

统计一个字符串在另外一个字符串出现次数 代码如下: package me.chunsheng.javatest; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by wei_spring on 16/10/11. * <p> * 统计一个字符串在另外一个字符串出现的次数 * 正则匹配的方法,前提字符串不是特殊字符串 * eg:finder("adadadadauuada&qu

【C语言】判断一个字符串是否是一个字符串的旋转字符串

//判断一个字符串是否是一个字符串的旋转字符串 //利用库函数实现 #include <stdio.h> #include <string.h> #include <assert.h> int IsRotate(char *str1, const char *str2) { assert(str1); assert(str2); strncat(str1, str1,strlen(str1)); if (NULL == strstr(str1, str2)) retur

js获取指定字符前/后的字符串简单实例

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head> <script type="text/javascript"> /* string 字符串; str 指定字符; split(),用于把一个字符串分割成字符串数组; split(str)[0],读取数组中索引为