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