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,以至于忘记了有简单的函数可以为您完成这项工作:)