我想将字符串的特定位置分组到字符串数组中。
示例:
string[] arr = {"DENVER01TECHNOLOGY",
"DENVER02TECHNOLOGY",
"DENVER01TECHNOLOGY",
"DENVER0103TECHNOLOGY",
"DENVER01TECHNOLOGY"};
我想根据第7位和第2位的位置对数组进行分组。即丹佛和技术之间的文本。
如何在C#中实现?
答案 0 :(得分:4)
var query = arr.GroupBy(i => i.Substring(6, 2));
// consider also using "DENVER".Length instead of 6
用法:
foreach (var g in query)
{
Console.WriteLine(g.Key);
foreach (var item in g)
Console.WriteLine(item);
}