Compare array list to a string, asp.net, c#

Hi,

I have the following code

 while (rdrEUrls.Read())
                            {
                                string requrls = rdrEUrls["requrl"].ToString();
                                GetalldUrlsFromDase.Add(requrl);


                            }

where GetalldUrlsFromDase is an array list

what i want to do is i have a string called X which has a value I want to loop through the whole array list and check if the value of x is in the array list. if it is then do nothing if it isnt then lbltext="Something there"

please help.

Thanks.

R8VI
R8VIAsked:
Who is Participating?
 
Richard LeeConnect With a Mentor Software EnthusiastCommented:
Below are two options
// Option 1
int index = alst.BinarySearch(X, delegate(object a, object b) { return String.Compare(a, b, true); });
 
if (index < 0)
{
    lbltext="Something there"
}
 
// Option 2
bool found = false;
foreach(string item in alst)
{
    if (String.Compare(X, item, true) == 0)
    {
        found = true;
        break;
    }
}
 
if (!found)
{
    lbltext="Something there"
}

Open in new window

0
 
angus_young_acdcCommented:
Try this (assuming your arraylist is full of strings, if not just do .To.String())
            string X = "The string you want to check for";
            foreach (string obj in myArrayListName)
            {
                if (X == obj)
                {
                    // do whatever processing you wish
                }
            }

Open in new window

0
 
Richard LeeSoftware EnthusiastCommented:
First two lines now included
ArrayList alst = new ArrayList();
string X = "a string";
 
// Option 1
int index = alst.BinarySearch(X, delegate(object a, object b) { return String.Compare(a, b, true); });
 
if (index < 0)
{
    lbltext="Something there"
}
 
// Option 2
bool found = false;
foreach(string item in alst)
{
    if (String.Compare(X, item, true) == 0)
    {
        found = true;
        break;
    }
}
 
if (!found)
{
    lbltext="Something there"
}

Open in new window

0
 
Göran AnderssonCommented:
Don't use ArrayList at all, it's practically obsolete. Use a generic list of strings:

List<string> urls = new List<string>();
while (rdrEUrls.Read()) {
   string requrls = rdrEUrls["requrl"].ToString();
   urls.Add(requrl);
}

Now you don't have to loop through the list yourself to look for a value, there are ready made methods for that:

if (!urls.Contains(X)) {
   lbltext="Something there"
}

If you are doing a lot of lookups in the list, you could use a dictionary instead, as it's a lot faster to look for a key in a dictionary:

Dictionary<string,int> urls = new Dictionary<string,int>();
while (rdrEUrls.Read()) {
   string requrls = rdrEUrls["requrl"].ToString();
   if (!url.ContainsKey(requrl)) {
      urls.Add(requrl, 0);
   }
}

Now checking for a string is practically instant, even if you would have a million strings in the dictionary.

if (!urls.ContainsKey(X)) {
   lbltext="Something there"
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.