How to Find character in ArrayList
-
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
-
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
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
-
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
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.
-
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
Not an answer to your question, but a suggestion. An
ArrayList
is generally not recommended as every item has to be converted to/from aobject
. 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 useList<T>
(whereT
is the type of your object, so for the example you have given useList<string>
. There are other collections inSystem.Collections.Generic
andSystem.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 asStringCollection
. 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)