ASP.NET - how to split string into array and check

Asked By msakt on 20-Jun-12 05:21 AM
s="1,2,3,4";

string arr[] = s.split(',');
if s=2

error occre in split..so now i check string s then split
DL M replied to msakt on 20-Jun-12 05:27 AM
About Split String function & how to use check this link.

http://www.ezineasp.net/post/ASP-Net-C-sharp-Split-String-Function.aspx
http://msdn.microsoft.com/en-us/library/system.string.split%28VS.80%29.aspx
Ravi Maurya replied to msakt on 20-Jun-12 05:31 AM
You can do like this where your variable s will hold string value

string str = "1,2,3,4";
    string[] strArray = str.Split(',');
    string strValue="2";
    foreach (string item in strArray)
    {
      if (item==strValue)
      {
        //Do your work
      }
    }
Try this and let me know
S K replied to msakt on 20-Jun-12 05:41 AM
When you split string than you will get array and you should access single value by array element as follows


s="1,2,3,4";

string arr[] = s.split(','); 
for(int i =0; i< arr.Length-1;i++)
{
if(arr[i].ToString() == "2")
{
//put your code here if element value is 2
}

}

hope this helps you
Jitendra Faye replied to msakt on 20-Jun-12 06:18 AM
Simply use this code-

s="1,2,3,4";

string arr[] = s.split(',');

foreach (string str in arr)
{
   if(str="your value")
    {
     //code
    }
}

Try this and let me know;.