用递归法判断字符串A中包含多少个字符串B
string类提供了判断字符串B在字符串A中首次(或最后)出现的Index的方法,但有时候需要判断B在A中出现了多少次。
为此想了一个算法。
public static void CountIndexOf1(string A, string B,int startindex,ref int count)
{ int j= A.IndexOf(B,startindex);
if (j <= )
return;
count++;
CountIndexOf(A, B, j+test.Length,ref count);
}
当然,为了方便,可以简单修改一下直接把上述方法扩展到string类:
public static class Extend //Extend类不能是内部类
{
public static void CountIndexOf(this string A, string B, int startIndex, ref int count)
{ int j = A.IndexOf(B, startIndex);
if (j <= )
return;
count++;
A.CountIndexOf(B, j + B.Length, ref count);
}
}
好了,来测试一下(完整代码):
class Program
{
static void Main(string[] args)
{
int i = , j = ;
string test = "samssamdamfdkamcdcdafdsamamasm";
CountIndexOf1(test, "am", , ref i);//查找test中含有多少个"am"
test.CountIndexOf("am", , ref j);//string类的扩展方法
Console.WriteLine("CountIndexOf1方法测试:包含am{0}个,应该为6个", i);
Console.WriteLine("扩展方法测试:包含am{0}个,应该为6个", j);
Console.Read();
}
public static void CountIndexOf1(string A, string B, int startindex, ref int count)
{ int j = A.IndexOf(B, startindex);
if (j <= )
return;
count++;
CountIndexOf1(A, B, j + B.Length, ref count);
}
}
public static class Extend
{
public static void CountIndexOf(this string A, string B, int startIndex, ref int count)
{ int j = A.IndexOf(B, startIndex);
if (j <= )
return;
count++;
A.CountIndexOf(B, j + B.Length, ref count);
}
}
测试结果:
用递归法判断字符串A中包含多少个字符串B的更多相关文章
- 判断DataTale中判断某个字段中包含某个数据
// <summary> /// 判断DataTale中判断某个字段中包含某个数据 /// </summary> /// <param name="dt&quo ...
- 算法 - 给出一个字符串str,输出包含两个字符串str的最短字符串,如str为abca时,输出则为abcabca
今天碰到一个算法题觉得比较有意思,研究后自己实现了出来,代码比较简单,如发现什么问题请指正.思路和代码如下: 基本思路:从左开始取str的最大子字符串,判断子字符串是否为str的后缀,如果是则返回st ...
- 纯JS实现在一个字符串b中查找另一个字符串a出现的所有位置,并且不使用字符串的方法(递归)
问题:判断字符串A在中所有出现字符串B中(长度大于1)的索引.不得使用字符串方法indexof,substring等 有小伙伴在面试遇到了这个问题,乍一看如果使用使用字符串方法indexof,subs ...
- java 判断Map集合中包含指定的键名,则返回true,否则返回false。
public static void main(String[] args) { Map map = new HashMap(); //定义Map对象 map.put("apple" ...
- sed打印包含一个字符串的行到包含另一个字符串的行解答
sed -n '/字符串1/,/字符串2/p' filename 这个命令为什么有时候打印不出来想要的东西,例如:sed -n '/root/,/adm/p' /etc/passwd 我 ...
- 写一段程序,删除字符串a中包含的字符串b,举例 输入a = "asdw",b = "sd" 返回 字符串 “aw”;一个容易被忽略的bug
代码如下: public class test{ public static void main(String args[]){ String test=test("sahsjkshabsh ...
- 判断某个字符串里面是否包含caoyang 这个字符串?
$string = 'Lorem ipsum dolor sit amet'; $preg = '/caoyang/'; $status = preg_match($preg, $string,$ma ...
- python 面试题 删除字符串a中包含的字符串b
- 请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始,每一步可以在矩阵中向左,向右,向上,向下移动一个格子。如果一条路径经过了矩阵中的某一个格子,则该路径不能再进入该格子。 例如 a b c e s f c s a d e e 矩阵中包含一条字符串"bccced"的路径,但是矩阵中不包含"abcb"路径,因为字符串的第一个字符b占据了矩阵中
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
随机推荐
- tensorflow版的bvlc模型
研究相关的图片分类,偶然看到bvlc模型,但是没有tensorflow版本的,所以将caffe版本的改成了tensorflow的: 关于模型这个图: 下面贴出通用模板: from __future__ ...
- 网站第三方登陆(一) QQ登陆
QQ 登陆是指,通过qq的用户名和密码,登录网站,不需要注册,网站的用户名和密码.如下图 一旦你授权了,就可以进入网站了,要实现这个效果 1.去qq互联平台(http://connect.qq.com ...
- sh
#/bin/bash#stop sms server smspid=`ps -aux |grep java |grep jPortMap |awk '{print $2}'`if [ "$ ...
- JAVA类与对象
Employee类: public class EmployeeTest { public static void main(String[] args) { // fill the staff ar ...
- SPI 驱动分析
断更博客两个月后我又回来了,眯着躺倒就能睡熟的小眼睛,在这儿敲键盘.这篇文章给你快乐,你有没有爱上我! SPI驱动由三部分组成,分别为drivers.core.device.通过bus总线连接.困了不 ...
- CSS设置DIV背景色渐变
div{ width: 200px; height: 200px; FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=, ...
- Java集合——ConcurrentHashMap
集合是编程中最常用的数据结构.而谈到并发,几乎总是离不开集合这类高级数据结构的支持.比如两个线程需要同时访问一个中间临界区(Queue),比如常会用缓存作为外部文件的副本(HashMap).这篇文章主 ...
- checkbox设置单选
http://blog.sina.com.cn/s/blog_4550f3ca010137td.html $("*") ‘表示获取所有对象 $("#XXX" ...
- 转:spl_autoload_register与autoload的区别详解
转:http://www.poluoluo.com/jzxy/201306/209614.html spl_autoload_register(PHP 5 >= 5.1.2)spl_autolo ...
- bzoj 3611: [Heoi2014]大工程
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #d ...