用递归法判断字符串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< ...
随机推荐
- hg0088新2网址
封装通俗的说,就是我有一些秘密不想让人知道,就通过私有化变量和私有化方法,这样外界就访问不到了.hg0088新2网址然后如果你有一些很想让大家知道的东西,你就可以通过this创建的属性看作是对象共有属 ...
- 使用Axis2建立WebService
Axis是apache重量级的WebService框架,虽然相比Xfire和CXF而言相对比较臃肿,但是企业中最常用的就是Axis,Axis2是Axis的升级版: 建立一个最简单的Axis2 W ...
- Webstorm 下的Angular2.0开发之路
人一旦上了年纪,记忆力就变得越来越不好. 最近写了许多的博文,倒不是为了给谁看,而是方便自己来搜索,不然一下子又忘记了. 如果恰巧帮助到了你,也是我的荣幸~~~~~~~~~~~~ 废话不多说,看正题~ ...
- 日期控件,layui
<link rel="stylesheet" href="<%=path%>/layui/css/layui.css" type=" ...
- 会计凭证BAPI_ACC_DOCUMENT_POST
*&---------------------------------------------------------------------* *& Report ZFIFB107 ...
- Groovy中文教程(链接收藏)
学习Gradle前,需要有一个Groovy语言的基础,以免被Groovy的语法困扰,反而忽略了Gradle的知识.这里有一个Groovy的简明中文教程文档,可以快速学习Groovy的一些语法:http ...
- UITableViewCell 顶格
首先在ViewDidLoad 或者ViewWillAppear里边写 if ([_tabView respondsToSelector:@selector(setSeparatorInset:)]) ...
- Nginx+php+fastcgi在win7下的配置
首先装载php 1.从www.php.net上下载php对应版本 2.解压之后放到c盘下(其实放哪无所谓,Apache会有配置指向,但是Nginx不用) 3.因为用的5.3.17版本,已经有了php- ...
- PHP发送电子邮件
1.导入文件,如本案例把Stmp.class.php放到Common\Common目录下,代码很多,直接复制就行! <?php namespace Common\Common; class Sm ...
- c# DataGridView 的一些属性设置,序号,合并头
this.dataGridView1.DataSource = this.dISASTERBindingSource; this.dataGridView1.Locatio ...