开发者

contains function in asp.net

i am trying to increase count by comparing two strings using Contains function. My code is,

  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;

After executing this,totalCount should be equal to 4 but it is giving me 5,means first time when it is checking condition for strSize.Contains(strM[s]) it is getting true instead of false. Can anybody tell me why this is happening.

Other thing when i am doing same in other application it is working fine.

code i wrote is,

 int count=0;
        string[] str = { "3", "4", "5", "10"};
        st开发者_StackOverflowring[] 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;

here o/p:totalCount=4. Can anybody tell me the difference between two.


strSize.Contains(strM[s]) returns true for 1, since 1 is contained in string 3,4,5,10


The difference is that the first code looks for a substring, while the second code compares whole strings.

The expression "3,4,5,10".Contains("1") returns true.


alright here is the deal, first code's strSize is just a simple string, second code's strSize is an array of string.

so when you say .contains for the first code it will look at the whole string and check if that you are looking for is there hence "3,4,5,10" contains 1 on it, moreover if you try strSize.indexof("10") in the first code it will give you 6. on the other hand if you try str.ToList().IndexOf("10) on the second code it will give you 3. alternatively if you use "1", first code will still return 6 but the second code will return -1 since it did not find an element "1" on the array.


Yes as Guffa said you are searching in a string so if that string contains you substring it returns true you should use your second method or fixed you code with something like this

 for (int s = 0; s < strM.Length; s++)
   { 
         stirng searchStr =  strM[s]+',';
if (strSize.Contains(searchStr)) { count++; chkSize.Items.FindByValue(strM[s]).Selected = true; } }

for this code works correctly you shouldn't remove the last ',' from strSize if your code is just wrote for this purpose.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜