2013-08-05 26 views
2

我想从一个数组只找到了第一个三个字符的字符串的索引查找的字符串(前三个字符)的索引如何从一个数组

我有一个月的数组

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个字符,即AUG如何找呢?

+0

您的意思是您想要查找数组中以给定字符串开头的第一个字符串的索引,例如, “AUG”? –

+1

您确实知道[DateTimeFormatInfo.GetMonthName](http://msdn.microsoft.com/library/system.globalization.datetimeformatinfo.getmonthname.aspx)和[DateTimeFormatInfo.GetAbbreviatedMonthName](http://msdn.microsoft.com/库/ system.globalization.datetimeformatinfo.getabbreviatedmonthname.aspx)? – Corak

+0

是.............................. – Proneet

回答

3

你有两种选择,我能想到的:

  1. Linq唯一的形式给出,看起来像这样:

    var index = arrayEnglishMonth.Select((v, i) => new { v, i }) 
              .Where(c => c.v.StartsWith("AUG")) 
              .Select(c => c.i) 
              .First(); 
    

    这将首先遍历现有阵列,营造可枚举的匿名持有值和索引的对象,其中在Where中传递的谓词返回true,然后选择唯一索引并从可枚举中获取第一个元素。

    Demo

  2. 查找相应月份使用Linq然后用IndexOf方法:

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

    Demo

+0

你在哪里使用't_caMonth'在你的答案?对问题中的代码进行更细心的观察。 –

+0

@AlexFilipovici据我了解,问题OP被困在'arrayEnlishMonth'中寻找索引。一旦他/她有索引,检查该索引是否存在于另一个数组中并不是什么大事。 – Leri

+1

关于1,它正在做其他事情:对数组只有1次迭代,对于每个元素,创建一个匿名对象,并且如果它以AUG开头,则返回其第二个元素。 – ytoledano

0

如果t_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_caMonth阵列创建List<string>

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

我试过这个,它不工作它给pos = -1 – Proneet

2

您可以用少许的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_caMonth.Length,而不是-1如果没有匹配的元素被发现。

+0

哪里是你的代码示例中的't_caMonth'变量? :) –

+0

@AlexFilipovici t_caMonth是我在运行时获得的一个数组 – Proneet

+0

@ p.s.w.g我想从数组中获取索引t_caMonth而不是数 – Proneet

3
arrayEnglishMonth.ToList().FindIndex(s => s.Substring(0,3) == "AUG"); 
+0

看起来很优雅,但我不知道如果迭代到达列表的末尾会发生什么。s.Substring(0,3)不会产生超出范围的异常? –

+0

为什么这是一个答案,如果他没有查看'arrayEnglishMonth',而是在't_caMonth'数组中? –

+0

@ G.Y。达到结尾,因为它没有找到一个匹配? -1返回 – wdavo

0

这是我的方式实现这一目标

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答案是使用以下更好

0

尝试,我认为这是最快的
Array.FindIndex(strArray, s => s.StartsWith("AUG"));

干杯。

+0

为什么ppl如此痴迷于linq,他们忘记了有简单的功能为你做这项工作:) –