views:

96

answers:

8

.NET 2

string[] myStrings = GetMyStrings();    
string test = "testValue";

How can I verify if myStrings contains or not test?

A: 
bool ContainsString(string[] arr, string testval)
{
    if ( arr == null )
        return false;
    for ( int i = arr.Length-1; i >= 0; i-- )
        if ( arr[i] == testval )
            return true;
    return false;
}

And this will have the best performance ever. :P

Vilx-
read attentively ;)
serhio
@serhio. I did. Thrice. You ask to determine whether or not a particular string is contained in the string array. This is how you do it. If this isn't what you need, please fix your question.
Vilx-
Just change the array from test to myStrings and this answer will work. How does it not work for you?
Chris Dunaway
Oh, wait, .NET 2.0. Well, same thing, just change the delegate syntax. OK, if you REALLY want it, I'll update my answer.
Vilx-
There, is THIS good enough?
Vilx-
@Vilx - It's not in VB! :P
Chris Dunaway
@Chris Dunaway - Why VB?
Vilx-
@Vilx - because the OP original had a VB.Net tag on the question. Now I see that it has been removed. I now see his original code is also C#. I missed that and was going by the tag.
Chris Dunaway
@Chris Dunaway - Actually he didn't. Check the edit history. Some other guy for reasons unknown added the VB.NET tag. OP removed it.
Vilx-
+2  A: 

Instead of using a static array, you could use a List:

List<string> myStrings = new List<string>(GetMyStrings());
if(myStrings.Contains("testValue"))
{
    // Do Work
}
Justin Niessner
I use `string[] letters = "a, b, c, d".Split();`. Is it optimal for such a elementary function to use conversions from Array to List?
serhio
@serhio - Code first, optimize later if needed. Unless you're constructing these lists on a large scale there really shouldn't be any problem.
Justin Niessner
A: 

I assume you want to check if any elements in your array contains a certain value (test). If so you want to construct a simple loop. In fact I think you should click here.

m.edmondson
+2  A: 

Here's a .NET 2.0 compliant approach. Using Array.Find will return null if the value isn't found.

C# Approach

string[] myStrings = { "A", "B", "testValue" };
string test = "testValue";
string result = Array.Find(myStrings, delegate(string s) { return s == test; });
Console.WriteLine("Result: " + result);

If you need a case insensitive match use s.Equals(test, StringComparison.InvariantCultureIgnoreCase).

EDIT: with VB.NET 2.0 more effort is required since it doesn't support anonymous delegates. Instead you would need to add a Function and use AddressOf to point to it. You would need to set the testValue as a member or property since it will not be passed in to the predicate method. In the following example I use Array.Exists.

VB.NET Approach

' field or property ' 
Dim test As String = "testValue"

Sub Main
    Dim myStrings As String() = { "A", "B", "testValue" }       
    Dim result As Boolean = Array.Exists(myStrings, AddressOf ContainsValue)
    Console.WriteLine(result)
End Sub

' Predicate method '
Private Function ContainsValue(s As String) As Boolean
    Return s = test
End Function
Ahmad Mageed
thanks for VB solution, even if I need C# one, maybe for VB people will be useful...
serhio
I missed the 2.0 and VB requirement. This will work, of course, but a call to Array.IndexOf might be simpler.
Chris Dunaway
@Chris agreed. This is closer to a `Contains` type of check but requires more effort than `IndexOf`.
Ahmad Mageed
+2  A: 

In .NET 2.0, you could do the following if you want the index:

int index = Array.FindIndex(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);

index will be -1 if myStrings does not contain test.

If you merely want to check for existence:

bool exists = Array.Exists(
    myStrings,
    delegate(string s) { return s.Equals(test); }
);
Jason
+1  A: 

Here is almost the exact same question on msdn. Find String in String Array As others have said you really have two options: 1) Use a list for easier checking 2) Loop through your array to find the string

Kyle C
A: 

How about this:

Sub Main
    Dim myStrings(4) As String
    myStrings(0) = "String 1"
    myStrings(1) = "String 2"
    myStrings(2) = "testValue"
    myStrings(3) = "String 3"
    myStrings(4) = "String 4"

    Dim test As String = "testValue"

    Dim isFound As Boolean = Array.IndexOf(myStrings, test) >= 0

    If isFound Then
        Console.WriteLine("Found it!")
    End If
End Sub

This should work for .Net 2.0 and VB.Net.

Chris Dunaway
The OP's issue is that it's not .NET 2.0 compatible with the lambdas.
Ahmad Mageed
+1  A: 

you can use Array.BinarySearch as described below.

 string[] strArray = GetStringArray();
        string strToSearch ="test";
        Array.BinarySearch(strArray, strToSearch);
saurabh
+1. The Generic variant may be even better.
serhio
It will only work if the array is sorted
Jan