asp.net 计算字符串中各个字符串出现的次数

太阳渐渐往下落,它的脸涨得越来越红,红的像个大火球,把身边的云染成五颜六色。慢慢地它走到西山背后,把美丽的霞光留在遥远的天边。我们都看得目瞪口呆。我的心里在想:晚霞真美!
实现第一步,需要把字符串分割为一个array,需要使用到的函数Split():
 
string[] arr = s.Split (',');

第二步,用Dictionary(TKey,TValue)实例化。
 
Dictionary<string, int> Statistics = new Dictionary<string, int>();

第三步,统计:
 
foreach (string w in arr)
{
if (Statistics.ContainsKey(w))
{
Statistics[w] += 1;
}
else
{
Statistics[w] = 1;
}
}

写完以上代码算是大功告成。
但Insus.NET还是要把统计的结果显示出来:
.aspx:
 
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" cellpadding="1" cellspacing="0">
<tr>
<td>字符 </td>
<td>次数 </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("key") %>
</td>
<td>
<%# Eval("value") %>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>

.aspx.cs:
 
protected void Page_Load(object sender, EventArgs e)
{
this.Repeater1.DataSource = Statistics;
this.Repeater1.DataBind();
}

结果:

如果你想看看MS SQL Server版本 可以查看 //www.haodaima.com/article/30212.htm

到此这篇关于asp.net 计算字符串中各个字符串出现的次数就介绍到这了。调节好兴奋期,学习一浪高一浪。更多相关asp.net 计算字符串中各个字符串出现的次数内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
ASP.NET轻量级MVC框架Nancy的基本用法

使用grpcui测试ASP.NET core的gRPC服务

ASP.NET Core中的对象池介绍

asp.net中MVC的处理流程详解

ASP.NET Core的日志系统介绍