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


    Mark this post as "ANSWER", if it helped you..
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 3:28 AM
    • Star
      9,222 point Star
    • satalaj
    • Member since 11-28-2007, 5:41 AM
    • Pune
    • Posts 1,826

    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
    • Contributor
      4,522 point Contributor
    • tompy_nation
    • Member since 02-28-2007, 10:45 AM
    • Gent
    • Posts 1,157

     

     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
    • Contributor
      2,254 point Contributor
    • dinesh_sp
    • Member since 12-03-2007, 5:41 AM
    • Melbourne
    • Posts 409

    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
    • Participant
      1,672 point Participant
    • rajeshthangarasu
    • Member since 03-14-2008, 7:08 AM
    • Chennai, India
    • Posts 254

     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 Technology Specialist (MCTS)
    Microsoft Certified Application Developer (MCAD)
  • 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


    Mark this post as "ANSWER", if it helped you..
  • Re: How to check a string is existing in a string array or not ?

    05-14-2008, 4:28 AM
    • Participant
      1,062 point Participant
    • arshadtop
    • Member since 01-22-2007, 5:02 PM
    • India
    • Posts 424

    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
    • Star
      9,222 point Star
    • satalaj
    • Member since 11-28-2007, 5:41 AM
    • Pune
    • Posts 1,826
    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
    • Member
      201 point Member
    • Elnath
    • Member since 03-12-2007, 10:45 AM
    • Posts 514

    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.

     


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How to check a string is existing in a string array or not ?

    05-16-2008, 1:11 AM
    • Member
      620 point Member
    • banoosuresh
    • Member since 06-26-2007, 8:05 AM
    • Posts 146

     try this out

    versions.contains(....); 

Page 1 of 1 (11 items)