What is the fastest way to compare a string with an array of strings in C#2.0
5 Answers
What kind of comparison do you want? Do you want to know if the given string is in the array?
bool targetStringInArray = array.Contains(targetString);
do you want an array of comparison values (positive, negative, zero)?
var comparisons = array.Select(x => targetString.CompareTo(x));
If you're checking for containment (i.e. the first option) and you're going to do this with multiple strings, it would probably be better to build a HashSet<string>
from the array:
var stringSet = new HashSet<string>(array);
if (stringSet.Contains(firstString)) ...
if (stringSet.Contains(secondString)) ...
if (stringSet.Contains(thirdString)) ...
if (stringSet.Contains(fourthString)) ...

- 1,421,763
- 867
- 9,128
- 9,194
You mean to see if the string is in the array? I can't remember if arrays support the .Contains() method, so if not, create a List< string >, add your array to the list via AddRange(), then call list.Contains({string to compare}). Will return a boolean value indicating whether or not the string is in the array.

- 2,180
- 3
- 19
- 29
-
1Correct, in 2.0 you would have to do `(new List
(array)).Contains(targetString);` - but from 3.5 you have the IEnumerable – vgru Jun 26 '09 at 21:17.Contains extension method, so it's available for arrays also. -
1In 2.0, you can check whether the array contains a specific value or not with a simple `Array.IndexOf(arr, val) >= 0`. – Mehrdad Afshari Jun 26 '09 at 21:35
If you are doing this many times with a single array, you should sort the array and binary search it:
Array.Sort(array);
int index = Array.BinarySearch(array, input);
// if (index < 0)
// does not exists, "items > ~index" are larger and "< ~index" are smaller
// otherwise, "items > index" are larger and "< index" are smaller.
Otherwise just check the whole array naively:
bool exists = Array.IndexOf(array, input) >= 0;

- 414,610
- 91
- 852
- 789
-
1If you're going to do it many times, there are more efficient searches than a binary search (assuming it's reasonably large, anyway). I'd go for something hash-based, e.g. HashSet. – Jon Skeet Jun 26 '09 at 21:08
-
1Yep. The hash table/binary search tree debate... Both are better than searching the whole array, nevertheless. HashSet is only available in 3.5 though. – Mehrdad Afshari Jun 26 '09 at 21:39
If your requirement to see if one list is a part of another, then you can use Contains().
Lets say
List<string> list1 = new List<string>(){"1", "2"};
List<string> list2 = new List<string>(){"1", "2", "3"};
list2.Contains(list1) //will be True, but not vice versa.
That said, if you want to know not partial match, but exact match, you can do use Except(), and check for remainder.
if(list2.Except(list1).Length == 0) //will return false.

- 1
- 1
//get data in list from source List checklist = Directory.GetFiles(SourcePath, ".", SearchOption.AllDirectories).Where(x => x.ToLower().EndsWith("apk")).ToList();
//get date from a text file
List<string> ls = ReadFile();
foreach(string file in checklist)
{
//get file name
string filename = Path.GetFileName(file);
string TargetLocation = Path.Combine(TargetPath, filename);
//now compare single string to a list
//it give in true and false
if(ls.Contains(filename))
{
//do your task
//File.Copy(file, TargetLocation);
}
}

- 77
- 1
- 3