【发布时间】: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
我的ViewData 中有一个字符串。我需要将其拆分为COMMA 并计算单词的出现次数。
i.e. ViewData["words"] = "apple,orange,grape".
我需要拆分并得到答案为 3。
【问题讨论】:
标签: asp.net asp.net-mvc-4 razor
您可以使用split 方法:
int count = ViewData["words"].Split(',').Length;
【讨论】:
函数Split 将字符串转换为字符串数组。
如果我们有string a="hello,bill,gates";,并在其上调用函数Split
string[] b = a.Split(',');,b 的值变为{"hello", "bill", "gates"}。
然后用Length统计元素个数:
int count = ViewData["words"].Split(',').Length;
【讨论】: