2013-08-05 23 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,他們忘記了有簡單的功能爲你做這項工作:) –