比较List <>中的两个对象并为其分配新值

时间:2018-12-27 09:55:38

标签: c#

如何比较列表中的两个对象?该列表包含具有两个属性Number和description的对象。这些数字如CC2和NS1,描述为Abcd,acd,...

列表的成员:CC1 Abcd,NU15 Abcd,CC2 Acd,NS1 Abd,CC21 Abd

onPostExecute()

我想做的是检查start是否有另一个以NS开头的数字(与end类似),检查Abd是否有另一个数字,以便两者都匹配(均以CC开头)。请记住,开始和结束的值采用这种格式,但由于输入了它们,因此与我分配的值不同,上面的值仅供参考。结果将是

string start = "NU15 Abcd";
string end = "NS1 Abd";

string startletters = string.Empty;
string startnumbers = string.Empty;

string endletters = string.Empty;
string endnumbers = string.Empty;

foreach (char c in start)
{
    if (Char.IsLetter(c))
    {
        startletters += c;
    }
    if (Char.IsNumber(c))
    {
        startnumbers += c;
    }
}
foreach (char c in end)
{
    if (Char.IsLetter(c))
    {
        endletters += c;
    }
    if (Char.IsNumber(c))
    {
        endnumbers += c;
    }
}
for (int i = 0; i < Program.file.Count; i++)
{
    if (startname == Program.file[i].Desc && start != Program.file[i].Number)
    { 
        start = Program.file[i].Number;      
    }
}

1 个答案:

答案 0 :(得分:0)

从您的问题中我了解到

//This will get you the first two characters of the string
string subS = start.Substring(0,2);
string subE = end.Substring(0,2);

//Do the comparison
if (subS == subE)
{
//your logic
}
else 
{
//another logic
}
相关问题