如何从字符串中删除逗号并将每个单词添加到C#中的集合中?
我正在尝试从以下字符串中删除逗号:如何从字符串中删除逗号并将每个单词添加到C#中的集合中?,c#,asp.net,C#,Asp.net,我正在尝试从以下字符串中删除逗号: string mystring = "One,Two,Three,Four"; 并在集合中添加每个单词 大概是一,二,三,四 我想要这个 NewsTag tag1 = new NewsTag(); tag1.Name = "one"; NewsTag tag2 = new NewsTag(); tag2.Name = "three"; NewsTag tag3 = new NewsTag(); tag3.Name = "three"; 谢谢试试
string mystring = "One,Two,Three,Four";
并在集合中添加每个单词
大概是一,二,三,四
我想要这个
NewsTag tag1 = new NewsTag();
tag1.Name = "one";
NewsTag tag2 = new NewsTag();
tag2.Name = "three";
NewsTag tag3 = new NewsTag();
tag3.Name = "three";
谢谢试试这个:
List<NewsTag> list = new List<NewsTag>();
list.Add(new NewsTag());
list.Add(NewsTag);
list.Add(NewsTag);
list.Add(NewsTag);
List<string> names = "One,Two,Three,Four".Split(',').ToList<string>();
for (var i = 0; i < names.Count; i++) {
list[i].Name = names[i];
}
List List=新列表();
添加(新的新闻标签());
添加(新闻标签);
添加(新闻标签);
添加(新闻标签);
List name=“一、二、三、四”。拆分('、')。ToList();
对于(var i=0;i
您的示例中没有逗号。请编辑您的帖子,使其更加具体,您的要求完全不清楚。string mystring=“一、二、三、四”;查找如何使用string.Replace()
的可能重复项还可以查看列表
以及如何添加/迭代值并添加到通用集合这就是谷歌
的目的。..@Lucia如果您了解我编写的代码,这将完全符合您的要求need@Lucia尝试var list=mystring.Split(',')。选择(n=>新的新闻标签{Name=n,}).ToList();
。