c# 如何判断字符串中相同字符串的个数
3个回答
展开全部
这个,还是比较方便的,利用定长改变的逻辑就可以了,效率也非常高。
写成了一个函数,便于调用
------------------------------------------
/// <summary>
/// 计算字符串中子串出现的次数
/// </summary>
/// <param name="str">字符串</param>
/// <param name="substring">子串</param>
/// <returns>出现的次数</returns>
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;
}
使用的方法:
--------------------------------------
string a = "abcdefababctr";
string b = "abc";
int c = SubstringCount(a, b); //这里的c就是b在a中出现的次数了
写成了一个函数,便于调用
------------------------------------------
/// <summary>
/// 计算字符串中子串出现的次数
/// </summary>
/// <param name="str">字符串</param>
/// <param name="substring">子串</param>
/// <returns>出现的次数</returns>
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;
}
使用的方法:
--------------------------------------
string a = "abcdefababctr";
string b = "abc";
int c = SubstringCount(a, b); //这里的c就是b在a中出现的次数了
展开全部
using System;
using System.Collections.Generic;
using System.Text;
namespace BuggyApp
{
class Program
{
static void Main(string[] args)
{
int count = 0;
int index=0;
string a = "babcdlsjfabcldsjfabcljkbc";
foreach (char s in a)
{
if (s.Equals('b'))
{
if (index < a.Length && index > 0)
{
if (a[index - 1].Equals('a') && a[index + 1].Equals('c'))
{
count++;
}
}
}
index++;
}
Console.WriteLine(count.ToString());
}
}
}
using System.Collections.Generic;
using System.Text;
namespace BuggyApp
{
class Program
{
static void Main(string[] args)
{
int count = 0;
int index=0;
string a = "babcdlsjfabcldsjfabcljkbc";
foreach (char s in a)
{
if (s.Equals('b'))
{
if (index < a.Length && index > 0)
{
if (a[index - 1].Equals('a') && a[index + 1].Equals('c'))
{
count++;
}
}
}
index++;
}
Console.WriteLine(count.ToString());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请参照以下代码:
private void button1_Click(object sender, EventArgs e)
{
string origeString = "aaabcdfererabcdfdfabcdfer";
GetAppearenceCount(origeString, "abc");
}
private int GetAppearenceCount(string origeString, string objectString)
{
int count = 0;
int objectStringLength = objectString.Trim().Length;
for (int i = 0; i < origeString.Length; i++)
{
int indexExists = origeString.IndexOf(objectString);
if (indexExists < 0)
break;
else
{
origeString = origeString.Trim().Substring(indexExists + objectStringLength, origeString.Trim().Length - indexExists - objectStringLength);
count++;
}
}
return count;
}
private void button1_Click(object sender, EventArgs e)
{
string origeString = "aaabcdfererabcdfdfabcdfer";
GetAppearenceCount(origeString, "abc");
}
private int GetAppearenceCount(string origeString, string objectString)
{
int count = 0;
int objectStringLength = objectString.Trim().Length;
for (int i = 0; i < origeString.Length; i++)
{
int indexExists = origeString.IndexOf(objectString);
if (indexExists < 0)
break;
else
{
origeString = origeString.Trim().Substring(indexExists + objectStringLength, origeString.Trim().Length - indexExists - objectStringLength);
count++;
}
}
return count;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询