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版本 可以查看 https://www.jb51.net/article/30212.htm