【问题标题】:Need to split a string and count occurrence需要拆分字符串并计算出现次数
【发布时间】:2019-08-23 09:05:57
【问题描述】:

我的ViewData 中有一个字符串。我需要将其拆分为COMMA 并计算单词的出现次数。

i.e. ViewData["words"] = "apple,orange,grape".

我需要拆分并得到答案为 3。

【问题讨论】:

  • 我希望你看到这个问题并不特定于 ASP.NET 和 ViewData。

标签: asp.net asp.net-mvc-4 razor


【解决方案1】:

您可以使用split 方法:

int count = ViewData["words"].Split(',').Length;

【讨论】:

    【解决方案2】:

    函数Split 将字符串转换为字符串数组。 如果我们有string a="hello,bill,gates";,并在其上调用函数Split string[] b = a.Split(',');b 的值变为{"hello", "bill", "gates"}。 然后用Length统计元素个数:

    int count = ViewData["words"].Split(',').Length;
    

    【讨论】:

      猜你喜欢
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2012-06-24
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多