Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Find character in ArrayList

How to Find character in ArrayList

Scheduled Pinned Locked Moved C#
tutorialcsharpc++data-structures
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raju_shiva
    wrote on last edited by
    #1

    Hi sir, In VC++ we use str.Find(); How can we use the same in C# Its working good in VC++ Here is the VC++ code i did:

    CString strvalue; // For Example strvalue is "A"
    CString strTemp; //strTemp is lists in array
    for()
    {
    if(strTemp.Find(strvalue)==0) // I will find "A" in array list
    m_ctrlCombo.AddString(strTemp); // If found i will add it in Combo
    }

    I am trying the same in C# But in C# we dnt have str.Find(),any other method to find it Here is wat i am trying

    ArrayList List = new ArrayList();

    List.Add("AAA");
    List.Add("AA");
    List.Add("BB");
    List.Add("BB");

    for (int i = 0; i < List.Count; i++)

    {

    string value = List[i] as string;
    if(value.Equals("AA"))
    comboBox1.Items.Add(value);
    }

    but i am getting all the value from ArrayList. Any idea Thanks Raj

    R K D 3 Replies Last reply
    0
    • R raju_shiva

      Hi sir, In VC++ we use str.Find(); How can we use the same in C# Its working good in VC++ Here is the VC++ code i did:

      CString strvalue; // For Example strvalue is "A"
      CString strTemp; //strTemp is lists in array
      for()
      {
      if(strTemp.Find(strvalue)==0) // I will find "A" in array list
      m_ctrlCombo.AddString(strTemp); // If found i will add it in Combo
      }

      I am trying the same in C# But in C# we dnt have str.Find(),any other method to find it Here is wat i am trying

      ArrayList List = new ArrayList();

      List.Add("AAA");
      List.Add("AA");
      List.Add("BB");
      List.Add("BB");

      for (int i = 0; i < List.Count; i++)

      {

      string value = List[i] as string;
      if(value.Equals("AA"))
      comboBox1.Items.Add(value);
      }

      but i am getting all the value from ArrayList. Any idea Thanks Raj

      R Offline
      R Offline
      ramzg
      wrote on last edited by
      #2

      You must try this out, if u really want to iterate :

      ArrayList List = new ArrayList();

      List.Add("AAA");
      List.Add("AA");
      List.Add("BB");
      List.Add("BB");
      foreach (string item in List)
      {
      if (item.Equals("AA"))
      comboBox1.Items.Add(item);
      }

      else, u can always go with this :

      ArrayList List = new ArrayList();

      List.Add("AAA");
      List.Add("AA");
      List.Add("BB");
      List.Add("BB");
      if (List.Contains("AA")
      comboBox1.Items.Add("AA");

      Ram

      1 Reply Last reply
      0
      • R raju_shiva

        Hi sir, In VC++ we use str.Find(); How can we use the same in C# Its working good in VC++ Here is the VC++ code i did:

        CString strvalue; // For Example strvalue is "A"
        CString strTemp; //strTemp is lists in array
        for()
        {
        if(strTemp.Find(strvalue)==0) // I will find "A" in array list
        m_ctrlCombo.AddString(strTemp); // If found i will add it in Combo
        }

        I am trying the same in C# But in C# we dnt have str.Find(),any other method to find it Here is wat i am trying

        ArrayList List = new ArrayList();

        List.Add("AAA");
        List.Add("AA");
        List.Add("BB");
        List.Add("BB");

        for (int i = 0; i < List.Count; i++)

        {

        string value = List[i] as string;
        if(value.Equals("AA"))
        comboBox1.Items.Add(value);
        }

        but i am getting all the value from ArrayList. Any idea Thanks Raj

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        If you you are using .net3.0 or higher you can use this:

        List list = new List();
        list.Add("AAA");
        list.Add("AA");
        list.Add("BB");
        list.Add("BB");

        //The code from you might find a better way of doing it depending upon your requirements!
        foreach (string found in list.FindAll(x=> x=="AA"))
        comboBox1.Items.Add(value);

        The above code is one way if achieving what you want. The generic list (equivalent to a c++ template) has a few advantages, the list is strongly typed (in this case all elements are strings). Additionally the generic list has more methods allowing you to find stuff in the list over the old ArrayList. The only thing I'd say (and it's a drawback in the code snippet I've added) is that the predicate syntax looks a little weird if you aren't used to it: list.FindAll(x=> x=="AA") See Generic List MSDN[^] for members of a generic lists if interested.

        Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

        1 Reply Last reply
        0
        • R raju_shiva

          Hi sir, In VC++ we use str.Find(); How can we use the same in C# Its working good in VC++ Here is the VC++ code i did:

          CString strvalue; // For Example strvalue is "A"
          CString strTemp; //strTemp is lists in array
          for()
          {
          if(strTemp.Find(strvalue)==0) // I will find "A" in array list
          m_ctrlCombo.AddString(strTemp); // If found i will add it in Combo
          }

          I am trying the same in C# But in C# we dnt have str.Find(),any other method to find it Here is wat i am trying

          ArrayList List = new ArrayList();

          List.Add("AAA");
          List.Add("AA");
          List.Add("BB");
          List.Add("BB");

          for (int i = 0; i < List.Count; i++)

          {

          string value = List[i] as string;
          if(value.Equals("AA"))
          comboBox1.Items.Add(value);
          }

          but i am getting all the value from ArrayList. Any idea Thanks Raj

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Not an answer to your question, but a suggestion. An ArrayList is generally not recommended as every item has to be converted to/from a object. This boxing/unboxing is an unecessary performance hit and more importantly can make working with the items (and therefore the code) more complicated. If the items are all of the same type then use List<T> (where T is the type of your object, so for the example you have given use List<string>. There are other collections in System.Collections.Generic and System.Collections.ObjectModel that you may find useful depending on what you are using the list for. I would recommend using the generic types where available or the ones that are type specific such as StringCollection. If you have mixed classes that you want in a collection but they derive from a common base class or interface then you can have a collection of that eg: List<IMyBaseInterface>.

          Dave

          If this helped, please vote & accept answer!

          Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups