C#在一组字符串中寻找另一组字符串出现的次数

qq_37591972 2017-04-19 09:48:16
编写一个函数,该函数可以统计一个长度不限的字符串在另一个字符串中出现的次数。例如,假定输入的字符串为asd asasdfg asd as zx67 asd mklo,子字符串为as,则应当输出6。如果输入的字符串为:asd asdasdljfl lfjdsaasd ljfd,子字符串位asd,则应当输出4。请赐教,谢谢!
...全文
1597 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_44448530 2021-04-05
  • 打赏
  • 举报
回复
string str = "abcabsbacaaaa"; int count = Regex.Matches(str, "aa", RegexOptions.Compiled).Count;
qq_30012045 2017-09-27
  • 打赏
  • 举报
回复
使用Linq using System.Linq; string tempString = "abcabsbacaaaa"; int aCoung = tempString.Count(x => x == 'a');
exception92 2017-04-20
  • 打赏
  • 举报
回复
拿去测试不谢。

       static int totalCount = 0;
        /// <summary>
        /// 获取重复次数
        /// </summary>
        /// <param name="value">传入的值</param>
        /// <param name="key">关键字</param>
        public static int GetRepetCount(string value, string key)
        {
            int totalValueLength = value.Trim().Length;
            int keyLength = key.Length;
            int startIndex = 0;
            int endIndex = startIndex + keyLength;
            for (int i = 0; i < totalValueLength; i++)
            {
                startIndex = i;
                endIndex = startIndex + keyLength;
                var temp = "";
                if (endIndex >= totalValueLength)
                {
                    temp = value.Substring(startIndex - 1);
                }
                else
                {
                    temp = value.Substring(startIndex, keyLength);
                }
                if (temp == key)
                {
                    totalCount++;
                }
            }
            return totalCount;
        }
白衣如花 2017-04-20
  • 打赏
  • 举报
回复
引用 6 楼 stherix 的回复:
我觉得是5次。。。
正怒月神 2017-04-20
  • 打赏
  • 举报
回复
如果要讲究效率,就实现kpm算法。 不然的话,楼上的方法都可用
stherix 2017-04-20
  • 打赏
  • 举报
回复
引用 4 楼 diaodiaop 的回复:
需要那么麻烦吗?

        var a = "asd asasdfg asd as zx67 asd mklo";
        var b = "as";
        var c = a.Split(new string[] { b }, StringSplitOptions.None).Length-1;
        Response.Write(c);

        a = "asd asdasdljfl lfjdsaasd ljfd";
        b = "asd";
        c = a.Split(new string[] { b }, StringSplitOptions.None).Length - 1;
        Response.Write(c);
任何人都能看懂的....结贴给分
这个不对 比如字符串aaaa 子串aa 应该是出现3次
㤁孞 2017-04-20
  • 打赏
  • 举报
回复
楼上正解!!!
by_封爱 版主 2017-04-20
  • 打赏
  • 举报
回复
需要那么麻烦吗?

        var a = "asd asasdfg asd as zx67 asd mklo";
        var b = "as";
        var c = a.Split(new string[] { b }, StringSplitOptions.None).Length-1;
        Response.Write(c);

        a = "asd asdasdljfl lfjdsaasd ljfd";
        b = "asd";
        c = a.Split(new string[] { b }, StringSplitOptions.None).Length - 1;
        Response.Write(c);
任何人都能看懂的....结贴给分
csdnFUCKINGSUCKS 2017-04-20
  • 打赏
  • 举报
回复

static int SubstringCount(string str, string substring)
{
    if (str.Contains(substring))
    {
        string strReplaced = str.Replace(substring, "");
        return (str.Length - strReplaced.Length) / substring.Length;
    }

    return 0;
}
zbdzjx 2017-04-20
  • 打赏
  • 举报
回复
public int StrCount(string a, string b)
{
    if (a.Trim() != "" && b.Trim() != "")
        return (a.Length - a.Replace(b, "").Length) / b.Length;
    else
        return 0;
}
qq_37591972 2017-04-20
  • 打赏
  • 举报
回复
引用 6 楼 stherix 的回复:
[quote=引用 4 楼 diaodiaop 的回复:] 需要那么麻烦吗?

        var a = "asd asasdfg asd as zx67 asd mklo";
        var b = "as";
        var c = a.Split(new string[] { b }, StringSplitOptions.None).Length-1;
        Response.Write(c);

        a = "asd asdasdljfl lfjdsaasd ljfd";
        b = "asd";
        c = a.Split(new string[] { b }, StringSplitOptions.None).Length - 1;
        Response.Write(c);
任何人都能看懂的....结贴给分
这个不对 比如字符串aaaa 子串aa 应该是出现3次 [/quote] 不不不,如果字符串是aaaa,子字符串是aa。那么应该出现是2次,不是三次。打印一次后,直接后移子字符串的长度。
stherix 2017-04-20
  • 打赏
  • 举报
回复
var str = "asd asasdfg asd as zx67 asd mklo";
var substr = "as";
int Count = 0;
for(int i = 0;i <= str.Length - substr.Length;)
{
	var index = str.IndexOf(substr, i);
	if(index<0) break;
	Count++;
	i += index+1;
}
fanbaoqiang111 2017-04-19
  • 打赏
  • 举报
回复
给分!
public int ReturnStringX(string a,string b)
        {
            int count = 0;
            string n = a;
            try
            {
                for (int i = 0; i <= a.Count(); i++)
                {
                    Console.WriteLine(n);
                    if (n.Substring(0, 3).Equals(b))
                    {
                        count++;
                        n = n.Remove(0, 3);
                    }
                    else
                    {
                        n = n.Remove(0, 1);
                    }
                }
            }catch(Exception ex) { }
            return count;
        }

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧