tags:

views:

8937

answers:

10

I need to search a string in the string array. I dont want to use any for looping in it

string [] arr = {"One","Two","Three"};

string var = "One"

I need to check whether var variable is present in arr.

+17  A: 

Well, something is going to have to look, and looping is more efficient than recursion (since tail-end recursion isn't fully implemented)... so if you just don't want to loop yourself, then either of:

bool has = arr.Contains(var); // .NET 3.5

or

bool has = Array.IndexOf(arr, var) >= 0;

For info: avoid named like var - this is a keyword in C# 3.0.

Marc Gravell
+5  A: 

Does it have to be a string[] ? A List<String> would give you what you need.

List<String> testing = new List<String>();
testing.Add("One");
testing.Add("Two");
testing.Add("Three");
testing.Add("Mouse");
bool inList = testing.Contains("Mouse");
ZombieSheep
+3  A: 

Each class implementing IList has a method Contains(Object value). And so does System.Array.

VolkerK
Note that with the 2.0 generic containers, this is on ICollection<T>; same idea though
Marc Gravell
A: 

Use "Any" quantifier for specify if a value exists in an array or not:

bool exists = arr.Contains("One");
mnour
That won't even compile; .Any() will tell you if it is non-empty; otherwise you'd need .Any(s=>s=="One"). It would be easier to use .Contains("One")
Marc Gravell
yeah.. correct .. it was just a quick answer.. I will update the answer..
mnour
+2  A: 

If the array is sorted, you can use BinarySearch. This is a O(log n) operation, so it is faster as looping. If you need to apply multiple searches and speed is a concern, you could sort it (or a copy) before using it.

GvS
A: 

At first shot, I could come up with something like this (but it's pseudo code and assuming you cannot use any .NET built-in libaries). Might require a bit of tweaking and re-thinking, but should be good enough for a head-start, maybe?

int findString(String var, String[] stringArray, int currentIndex, int stringMaxIndex)
    {
    if currentIndex > stringMaxIndex 
       return (-stringMaxIndex-1);
    else if var==arr[currentIndex] //or use any string comparison op or function
       return 0;
    else 
       return findString(var, stringArray, currentIndex++, stringMaxIndex) + 1 ;
    }



    //calling code
    int index = findString(var, arr, 0, getMaxIndex(arr));

    if index == -1 printOnScreen("Not found");
    else printOnScreen("Found on index: " + index);
Salman Kasbati
A: 

In C#, if you can use an ArrayList, you can use the Contains method, which returns a boolean:

if MyArrayList.Contains("One")
DOK
in .NET 2.0, always prefer List<T> over ArrayList. A List<string> would do the same, but with better type-safety, and more flexibility.
Marc Gravell
+1  A: 

Why the prohibition "I don't want to use any looping"? That's the most obvious solution. When given the chance to be obvious, take it!

Note that calls like arr.Contains(...) are still going to loop, it just won't be you who has written the loop.

Have you considered an alternate representation that's more amenable to searching?

  • A good Set implementation would perform well. (HashSet, TreeSet or the local equivalent).
  • If you can be sure that arr is sorted, you could use binary search (which would need to recurse or loop, but not as often as a straight linear search).
bendin
+1  A: 

Every method, mentioned earlier does looping either internally or externally, so it is not really important how to implement it. Here another example of finding all references of target string

       string [] arr = {"One","Two","Three"};
       var target = "One";
       var results = Array.FindAll(arr, s => s.Equals(target));
Tamir
A: 

I think it is better to use Array.Exists than Array.FindAll.

Joe Cheri Ross
It's a little bit old question that already had been answered...
Tomek Tarczynski