C# 有没有比在字符串数组中查找字符串更快的方法?

C# 有没有比在字符串数组中查找字符串更快的方法?,c#,arrays,visual-studio-2010,list,C#,Arrays,Visual Studio 2010,List,目前我正在这样做: List<int> ID = new List<int>(); int a = 0; string[] findWordHere = ... string[] findOtherWordHere = ... string word = "this"; string otherWord = "that"; foreach (string wo

目前我正在这样做:

        List<int> ID = new List<int>();
        int a = 0;
        string[] findWordHere = ...
        string[] findOtherWordHere = ...
        string word = "this";
        string otherWord = "that";

        foreach (string word in findWordHere)
        {
            foreach (string otherWord in findOtherWordHere)
            {
                if (word == otherWord)
                {
                    ID.Add(a + 1);
                }
            }
            a++;
        }
List ID=new List();
int a=0;
字符串[]findWordHere=。。。
字符串[]findOtherWordHere=。。。
string word=“this”;
string otherWord=“that”;
foreach(findWordHere中的字符串字)
{
foreach(findOtherWordHere中的字符串otherWord)
{
if(word==otherWord)
{
ID.Add(a+1);
}
}
a++;
}

这非常耗时。有没有更好的方法来实现这一点,比如使用linq?

您可以使用
哈希集

var set=newhashset(findOtherWordHere);
var id=findWordHere
.Select((w,i)=>new{w,i})
其中(p=>set.Contains(p.w))
.选择(p=>p.i)
.ToList();

这提供了包含在另一个列表中的单词的索引,没有循环,并且性能合理。

您可以使用
HashSet

var set=newhashset(findOtherWordHere);
var id=findWordHere
.Select((w,i)=>new{w,i})
其中(p=>set.Contains(p.w))
.选择(p=>p.i)
.ToList();

这提供了包含在其他列表中的单词的索引,没有循环,性能合理。

是的,有。将
findOtherWordHere
中的所有项添加到第一个循环之前的
HashSet
,并将其用于查找,如下所示:

ISet<string> lookup = new HashSet<string>(findOtherWordHere);
foreach (string word in findWordHere)
{
    if (lookup.Contains(word))
    {
        ID.Add(a + 1);
    }
    a++;
}
ISet lookup=新哈希集(findOtherWordHere);
foreach(findWordHere中的字符串字)
{
if(lookup.Contains(word))
{
ID.Add(a+1);
}
a++;
}
这与
findWordHere
findOtherWordHere
的长度之和成比例运行,因为
HashSet
内置于线性时间中,并提供恒定时间查找功能


另一方面,您最初的方法在时间上与
findWordHere
findOtherWordHere
长度的乘积成正比,因为最坏的情况是在外循环的每次迭代中执行一个内循环以完成。

是的,有。将
findOtherWordHere
中的所有项添加到第一个循环之前的
HashSet
,并将其用于查找,如下所示:

ISet<string> lookup = new HashSet<string>(findOtherWordHere);
foreach (string word in findWordHere)
{
    if (lookup.Contains(word))
    {
        ID.Add(a + 1);
    }
    a++;
}
ISet lookup=新哈希集(findOtherWordHere);
foreach(findWordHere中的字符串字)
{
if(lookup.Contains(word))
{
ID.Add(a+1);
}
a++;
}
这与
findWordHere
findOtherWordHere
的长度之和成比例运行,因为
HashSet
内置于线性时间中,并提供恒定时间查找功能


另一方面,您最初的方法在时间上与
findWordHere
findOtherWordHere
长度的乘积成正比,因为最坏的情况是在外循环的每次迭代中执行一个内循环以完成。

如果您不需要数组,可以使用
StringComparison
方法:

            List<int> ID = new List<int>();

            int a = 0;

            string root = "that";
            string root2 = "that";

            bool result = root.Equals(root2, StringComparison.Ordinal);


            if (result)
            {
               ID.Add(a + 1);
            }

如果不需要数组,可以使用
StringComparison
方法:

            List<int> ID = new List<int>();

            int a = 0;

            string root = "that";
            string root2 = "that";

            bool result = root.Equals(root2, StringComparison.Ordinal);


            if (result)
            {
               ID.Add(a + 1);
            }

一个简单的普通for循环会更快一个简单的普通for循环会更快一个简单的普通for循环应该在C#中工作还是应该是HashSet?@user2340818应该是
ISet
@user2340818我很抱歉输入错误,Magnus是对的,我错过了接口名称前面的
I
前缀
HashSet
var
在这里也能很好地工作。Set应该在C中工作还是应该是HashSet?@user2340818应该是
ISet
@user2340818我很抱歉输入错误,Magnus是对的,我错过了接口名称前面的
I
前缀<代码>哈希集或
变量
在这里也能很好地工作。@Magnus谢谢-修复。@Magnus谢谢-修复。