在 C#2.0 中将字符串与字符串数组进行比较的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1051276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way to compare a string with an array of strings in C#2.0
提问by Greens
What is the fastest way to compare a string with an array of strings in C#2.0
在 C#2.0 中将字符串与字符串数组进行比较的最快方法是什么
采纳答案by James McConnell
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.
你的意思是看看字符串是否在数组中?我不记得数组是否支持 .Contains() 方法,所以如果不支持,创建一个 List<string>,通过 AddRange() 将你的数组添加到列表中,然后调用 list.Contains({string to compare})。将返回一个布尔值,指示字符串是否在数组中。
回答by Mehrdad Afshari
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;
回答by Jon Skeet
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:
如果您正在检查是否包含(即第一个选项)并且您打算使用多个字符串执行此操作,那么HashSet<string>
从数组构建一个可能会更好:
var stringSet = new HashSet<string>(array);
if (stringSet.Contains(firstString)) ...
if (stringSet.Contains(secondString)) ...
if (stringSet.Contains(thirdString)) ...
if (stringSet.Contains(fourthString)) ...
回答by kirti kant pareek
//get data in list from source List checklist = Directory.GetFiles(SourcePath, ".", SearchOption.AllDirectories).Where(x => x.ToLower().EndsWith("apk")).ToList();
//从源列表中获取列表 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);
}
}