C# 如何比较每个字符串使用的两个字符串列表
我试图使用此代码循环浏览2个列表C# 如何比较每个字符串使用的两个字符串列表,c#,C#,我试图使用此代码循环浏览2个列表 foreach (string str1 in list<string>tags) { foreach (string str2 in list<string>ArchivedInformation) { } } foreach(listtags中的字符串str1) { foreach(listArchivedInformation中的字符串str2){} } 第一个列表包含存储为“tag1、tag2、tag3..”的标记
foreach (string str1 in list<string>tags)
{
foreach (string str2 in list<string>ArchivedInformation) { }
}
foreach(listtags中的字符串str1)
{
foreach(listArchivedInformation中的字符串str2){}
}
第一个列表包含存储为“tag1、tag2、tag3..”的标记,第二个列表包含存储为“tag1、Datestamp、0.01”、“tag2、Datestamp、0.02”等的信息
我想问一下,如何获得第二个列表中的标签,并将其作为第一个列表的条件?我已尝试拆分第二个列表,但我无法获得确切的“Tag1”作为id以将其用作条件
最后,我想做的目标是
Str1(来自标签列表)==Str2(来自归档信息)
一切皆有可能。另一件事是,如果它是理智的:)
public void Something()
{
//使用字典
var dict=新字典();
dict.Add(“tag1”、“tag1,日期戳,0.01”);
dict.Add(“tag2”,“tag2,日期戳,0.02”);
//out->“tag2,日期戳,0.02”
系统.诊断.调试.写线(dict[“tag2]”);
//使用两个单独的列表
var tags=新列表{“tag1”、“tag2”};
var infos=新列表{“tag1,日期戳,0.01”,“tag2,日期戳,0.02”};
//out->tag1,日期戳,0.01和tag2,日期戳,0.02
tags.ForEach(tag=>
System.Diagnostics.Debug.WriteLine(
infos.First(info=>info.StartsWith(tag));
}
你考虑过使用字典吗?我没有,谢谢你的建议。我只是想知道,仅仅使用字典是可行的吗?或者也可以使用列表来完成。你熟悉字典吗?它是一个包含键和相关值列表的数据结构。我会构造您的数据,以便标记是字典键,ArchivedInformation值是字典值。您不需要在值中包含标记,因此它就像“Datestamp,0.01”。你可以用字典。不过,我不太确定我是否理解您对foreach循环的实际操作。您似乎只是在尝试关联数据,如果您没有使用两个列表,您甚至可能不需要。是的,我实际上是在同时访问两个列表,因为这是系统的当前设计。但我肯定需要看一本字典,因为他们的偏好是访问两个列表并比较两个列表上的id。非常感谢!谢谢我试图使用这个函数以及字符串cmp;cmp=str2.Substring(0,str2.IndexOf(',');
public void Something()
{
// Using Dictionary
var dict = new Dictionary<string, string>();
dict.Add("tag1", "tag1,datestamp,0.01");
dict.Add("tag2", "tag2,datestamp,0.02");
// out -> "tag2,datestamp,0.02"
System.Diagnostics.Debug.WriteLine(dict["tag2"]);
// Using two separate lists
var tags = new List<string> { "tag1", "tag2" };
var infos = new List<string> { "tag1,datestamp,0.01", "tag2,datestamp,0.02" };
// out -> tag1,datestamp,0.01 & tag2,datestamp,0.02
tags.ForEach(tag =>
System.Diagnostics.Debug.WriteLine(
infos.First(info => info.StartsWith(tag))));
}