[英].split with razor details C# asp.net core
Want to take the string NPCSkills and have it split into a list 想要采用字符串NPCSkills并将其拆分为列表
NPCSkills is a prop that includes a string like "guns = 4, armor = 2"
I want it to split into a list so I can display it NPCSkills是一种道具,其中包括
"guns = 4, armor = 2"
类的字符串"guns = 4, armor = 2"
我希望将其拆分为一个列表,以便可以显示它
I am doing the .split but not sure what to do now. 我正在执行.split,但不确定现在要做什么。
string[] Words = (Model.NPCSkills).Split(',');
foreach (var item in Words)
{
<li>
@item.Words
</li>
}
string
does not contain a definition for 'Words' and no extension method 'Words' accepting a first argument of type string
could be found (are you missing a using directive or an assembly reference?) + @item.Words string
不包含'Words'的定义,也找不到扩展方法'Words'接受string
类型的第一个参数(您是否缺少using指令或程序集引用?)+ @ item.Words
You could use the following code to convert the NPCSkills
( comma-separated string
) to a strongly-typed Dictionary
: 您可以使用以下代码将
NPCSkills
(以comma-separated string
)转换为强类型的Dictionary
:
var Words = (Model.NPCSkills)
.Split(',')
.Select(s => s.Split("="))
.ToDictionary(arr => arr[0], arr =>arr[1] );
Or if you would like to remove the space around the key/value pair : 或者,如果您想删除键/值对周围的空格,请执行以下操作:
var Words = (Model.NPCSkills)
.Split(',')
.Select(s => s.Split("=").Select(e => e.Trim()).ToArray())
.ToDictionary(arr => arr[0], arr =>arr[1]);
Now you could render it in a safer way: 现在,您可以以更安全的方式渲染它:
@foreach(var item in Words)
{
<li>
<span class="badge">@item.Key</span>
<span class="text">@item.Value</span>
</li>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.