How to check a string is existing in a string array or not ?

Last post 05-16-2008 1:11 AM by banoosuresh. 10 replies.

Sort Posts:

  • How to check a string is existing in a string array or not ?

    05-14-2008, 3:00 AM

    HI,

    i am in a deep trouble.

    here i have declared a string array..

            int arrayCount = Convert.ToInt32(Session["VersionIds"].ToString().Split(',').Length - 1);
          
            string[] versions = new string[arrayCount];

    in this array, i wanna check wheather a string is there or not...

    if it dosent exists i wanna add that string to the string array......

    for that i tried to use

    version[arrayCount].Contains(..........);

    but this is thowing an exception...... index out of range..

     

    can anyone please tell me how cann i solve this issue... 


        
     

     

     

     



    Thanks and Regards
    Nimesh Prabhakar
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 3:28 AM
    • Loading...
    • satalaj
    • Joined on 11-28-2007, 5:41 AM
    • Pune
    • Posts 874

    string []arrString = "Hello ASp.net Brothers".Split(" ").ToCharArray();

    foreach(string str in arrString)
    {
      
       if(str.Equals("your strin to comapre")
        {
    / //   Do something 
         }

    }

    I hope this snippet will help you
    Satalaj

  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 3:37 AM
    Answer

     

     First error (index out of range is solvable doing this:

     

    version[arrayCount - 1].Contains(..........);

     

     

     

    Check possibility: 

    string wordToCheck = "Test";

    bool found=false;

    string[] versions = new string[arrayCount];foreach (string rule in versions)

    {

    if (rule.Equals(wordToCheck))

    {

    found =
    true;

    }

    }

    if (!found)

    {

    versions[0] = wordToCheck;

    }

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 3:49 AM
    • Loading...
    • dinesh_sp
    • Joined on 12-03-2007, 5:41 AM
    • Melbourne
    • Posts 404

    Hi,

    It would be troublesome to add to array, as it is fixed size, instead it would be easier to use arraylist. Here is the code you are looking for:-


    ArrayList al = new ArrayList();
    al.Add("hello");
    al.Add("abcd");
    al.Add("This");
    al.Add("Is");
    al.Add("My");
    al.Add("New");
    al.Add("ArrayList");
    al.Add("Control");
    for (int i = 0; i <= al.Count - 1; i++) {
     if (al.Item(i).ToString == "hello")
     {
      continue;
     }
     else
     {
      al.Add(al.Item(i).ToString);
     }

    }
    for (int j = 0; j <= al.Count - 1; j++) {
     Response.Write((string)al.Item(j));
     Response.Write("&nbsp;<->&nbsp;");
    }

     

  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 3:50 AM

     Try this code:

     

            string[] arr = new string[3];
            arr[0] = "India";
            arr[1] = "America";
            arr[2] = "China";
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[i] == "China")
                {
    			//----------------your code here-----------------
                }
            } 
     
    Mark as Answer if the post was useful to you

    Rajesh Thangarasu
    Microsoft Certified Professional
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 4:28 AM

     

    satalaj:

    string []arrString = "Hello ASp.net Brothers".Split(" ").ToCharArray();

    foreach(string str in arrString)
    {
      
       if(str.Equals("your strin to comapre")
        {
    / //   Do something 
         }

    }

    I hope this snippet will help you
    Satalaj

     

    in the first strp itself i wanna check :

     if(str.Equals("your strin to comapre")

    {

    if false--- it should go inside.. when ever the string exists in the array, it shoul;d not do anything.

    that is if the araay does nt contain the string which we are comparing, it should go inside.... else nothing...

     



    Thanks and Regards
    Nimesh Prabhakar
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 4:28 AM
    • Loading...
    • arshadtop
    • Joined on 01-22-2007, 5:02 PM
    • India
    • Posts 255

    Hi,

    private void Button1_Click(object sender, System.EventArgs e)
    {
     void[] arr;
     string str;
     str = "sample text contains commas and this can be done in this way";
     arr = str.Split(" ".ToCharArray);
     object compstring = "commas";
     for (int i = 0; i <= (arr.Length) - 1; i++) {
      if ((string.Compare(arr(i), compstring)) == 0)
      {
       MsgBox("String is Equal");
      }
     }
    }


    Regards

    Arshad

    Please Mark as Answer if the post is helpful to you.
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 5:31 AM
    • Loading...
    • satalaj
    • Joined on 11-28-2007, 5:41 AM
    • Pune
    • Posts 874
    Ok do this way

    System.Collections.Generic.
    List<string> lstString = new System.Collections.Generic.List<string>();

    lstString = "Hello Asp.net Brothers".Split(" ").ToCharArray();

    foreach(String str in lstString)
    {
          if(str == "yourString")
            {
            //do something 
            }
           else
            {
             lstString.Add("yourString");
            }

    }

    Satalaj

  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 5:42 AM
    • Loading...
    • Elnath
    • Joined on 03-12-2007, 10:45 AM
    • Posts 420

    hello, for this purpose you should declare a List<string> instead of an array, this allow you to search for elements, you cna later convert the list to an array if for any particular reason you really need an array.

  • Re: How to check a string is existing in a string array or not ?

    05-15-2008, 11:26 PM
    Answer

    Hi,

    The count of array is arrayCount. So the first item is version[0] and the last item is version[arrayCount-1].

    version[arrayCount] is out of range so as to throw a error. The number should be no more than arrayCount-1.

    Hope it helps.

     

    Sincerely,
    Vince Xu
    Microsoft Online Community Support
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as
    Answer” if a marked post does not actually answer your question.
  • Re: How to check a string is existing in a string array or not ?

    05-16-2008, 1:11 AM

     try this out

    versions.contains(....); 

Page 1 of 1 (11 items)