我想通过使用Contains函数比较两个字符串来增加计数。 我的代码,包含asp.net中的函数
int count = 0;
string strSize = "";
for (int i = 0; i < dts.Rows.Count; i++)
{
strSize += dts.Rows[i]["sizeid"].ToString() + ",";
}
if (strSize.Length > 0)
strSize = strSize.Substring(0, strSize.Length - 1);
// After executing strSize="3,4,5,10"
if (strMainSize.Length > 0)
strMainSize = strMainSize.Substring(0, strMainSize.Length - 1);
// After executing strMainSize ="1,2,3,4,5,10,45"
strM = strMainSize.Split(',');
for (int s = 0; s < strM.Length; s++)
{
if (strSize.Contains(strM[s]))
{
count++;
chkSize.Items.FindByValue(strM[s]).Selected = true;
}
}
var totalCount = count;
执行在此之后,TOTALCOUNT应该等于4但它给我5,是指当它被用于strSize.Contains检查条件(STRM [S])它是获得第一次真实而不是错误。 有人可以告诉我为什么会发生这种情况。
其他的事情,当我在其他应用程序中做同样的工作正常。
代码我写是,
int count=0;
string[] str = { "3", "4", "5", "10"};
string[] strM = {"1","2","3","4","5","10","45","50" };
for (int s = 0; s < strM.Length; s++)
{
var stM = strM[s];
if (str.Contains(strM[s]))
{
count++;
chkSize.Items.FindByValue(strM[s]).Selected = true;
}
}
int totalCount = count;
这里O/P:TOTALCOUNT = 4。 任何人都可以告诉我两者之间的区别。