Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从数组中查找字符串(前三个字符)的索引_C#_.net - Fatal编程技术网

C# 如何从数组中查找字符串(前三个字符)的索引

C# 如何从数组中查找字符串(前三个字符)的索引,c#,.net,C#,.net,我只想为数组的前三个字符找到字符串的索引 我有一个月的数组 string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" }; 如果我写 int t_ciMonth=8;(AUGUST) int pos = Array.IndexOf(

我只想为数组的前三个字符找到字符串的索引

我有一个月的数组

string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
如果我写

     int t_ciMonth=8;(AUGUST)
     int pos = Array.IndexOf(t_caMonth, arrayEnglishMonth[t_ciMonth - 1]);

但是,如果我只需要前3个字符的索引,即如何找到它?

如果
t\u caMonth
具有大写字母且其值只有3个字母,则可以使用:

 int pos = Array.IndexOf(t_caMonth, arrayEnglishMonth[t_ciMonth - 1]
     .Substring(0,3));
为了管理超过3个字符的大小写和值,您可以:

var pos = -1;
var sel = t_caMonth
    .Select((i, index) => new { index, i = i.ToUpper() })
    .Where(i => 
        i.i.Substring(0,3) == arrayEnglishMonth[t_ciMonth - 1].Substring(0, 3))
    .Select(i => i.index);
if (sel.Count() > 0)
    pos = sel.FirstOrDefault();
您也可以从
t\u caMonth
数组创建
列表

var pos2 = t_caMonth
    .ToList()
    .FindIndex(i => 
        i.ToUpper().Substring(0, 3) == 
            arrayEnglishMonth[t_ciMonth - 1].Substring(0, 3));

您可以使用一点Linq来完成此操作:

string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
string[] t_caMonth = { ... };
string search = arrayEnglishMonth[7].Substring(0, 3); // "AUG";
int pos = t_caMonth
    .Select((s, i) => new { s, i }).Dump()
    .Where(x => x.s == search)
    .Select(x => x.i)
    .DefaultIfEmpty(-1).First();
或者更简单地说:

int pos = t_caMonth.TakeWhile(s => s != search).Count();

虽然如果找不到匹配的元素,最后一个解决方案将返回
t\u caMonth.Length
而不是
-1

我可以想到两个选项:

arrayEnglishMonth.ToList().FindIndex(s => s.Substring(0,3) == "AUG");
  • Linq
    只有这样一种方法:

    var index = arrayEnglishMonth.Select((v, i) => new { v, i })
                                 .Where(c => c.v.StartsWith("AUG"))
                                 .Select(c => c.i)
                                 .First();
    
    这将首先迭代现有数组,创建包含值和索引的匿名对象的可枚举性,其中传入的谓词
    where
    返回true,然后仅选择索引并从可枚举性中获取第一个元素

  • 使用
    Linq
    查找相应月份,然后使用
    IndexOf
    方法:

    var item = arrayEnglishMonth.First(c => c.StartsWith("AUG"));
    var index = Array.IndexOf(arrayEnglishMonth, item);
    


  • 这是我实现这一目标的方法

    string[] arrayEnglishMonth = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
    var searcheditem = arrayEnglishMonth.FirstOrDefault(item => item.Substring(0, 3) == "AUG");
    if(searcheditem != null)
    var itemIndex = Array.IndexOf(arrayEnglishMonth, searcheditem);
    

    但是@wdavo答案更好

    试试下面的方法,我认为这是最快的
    Array.FindIndex(strArray,s=>s.StartsWith(“AUG”)


    干杯。

    您的意思是要查找数组中以给定字符串开头的第一个字符串的索引,例如“AUG”?您知道和吗?是的,什么是
    t\u caMonth
    ?是某种C变量命名约定吗?@AlexFilipovici t\u caMonth是我在运行时得到的数组time@p.s.w.g我想从数组t_caMonth而不是count@Proneet对不起,我误解了你要找的东西。我已经更新了我的答案。但是,第二个示例中的
    计数
    并没有达到您认为的效果
    TakeWhile(…).Count()
    遍历上的列表,直到找到不符合条件的元素(在本例中,直到找到一个等于
    search
    的字符串)然后返回在找到一个匹配项之前必须经过的项数,即第一个匹配项的索引。看起来很优雅,但我想知道如果迭代到达列表的末尾会发生什么。。s、 子字符串(0,3)不会产生超出范围的异常?如果他没有查看
    arrayEnglishMonth
    ,而是在
    t\u Cammonth
    数组中,为什么这是一个答案?@G.Y.到达末尾,因为在中没有找到匹配项-返回1您在回答中在哪里使用
    t\u caMonth
    ?请更仔细地查看问题中的代码。@AlexFilipovici据我所知,问题OP一直在
    arrayEnlishMonth
    中查找索引。一旦他/她有了索引,检查该索引是否存在于另一个数组中就没什么大不了的了。关于1,它在做其他事情:在数组上只有一次迭代,对于每个元素,创建一个匿名对象,如果它以AUG开头,则返回第二个元素。@ytoledano是的,你说得对<代码>其中SelectEnumerableIterator
    将用于对数组进行迭代。我会更新答案。为什么ppl如此痴迷于linq,以至于忘记了有简单的函数可以为您完成这项工作:)