我有一个字符串,它的分隔格式如下:
组织名称:名字姓氏,名字姓氏;
(这可以重复每个组织的组织名称和可变数量的名称)
例:
**XXX University**: Martha Zander, Rick Anderson; **Albert School**: Nancy Vanderburg, Eric Towson, George Branson; **Hallowed Halls**: Jane Goodall, Ann Crabtree, Rick Grey, Tammy Hudson;
生成的字符串需要按组织名称进行分组和排序,然后按姓氏排序,如下所示:
**Albert School**: George Branson, Eric Towson, Nancy Vanderburg;
**Hallowed Halls**: Ann Crabtree, Jane Goodall, Rick Grey, Tammy Hudson;
**XXX University**: Rick Anderson, Martha Zander;
我已经走到了这一步:
string[][] splitThis = staff.Split(';').Select(s => s.Split(':')).ToArray();
这给了我一个按 orgname 拆分的数组,然后我就卡在这之后了。
我知道我需要一个带有 orname、名字、姓氏的多维数组,但我不知道如何填充、分组和排序以获得正确的结果。
public static void Main()
{
var input =
@"**Albert School**: George Branson, Eric Towson, Nancy Vanderburg;
**Hallowed Halls**: Ann Crabtree, Jane Goodall, Rick Grey, Tammy Hudson;
**XXX University**: Rick Anderson, Martha Zander;";
var universities = input
.Split(';')
.Select(ParseUniversity)
.ToArray();
}
public static University ParseUniversity(string line)
{
var fields = line
.Split(',',':')
.Select(f=>f.Trim('*','n','r', 't' ,' ')) // remove trailing crap
.ToArray();
var universityName = fields.First();
var persons = fields
.Skip(1) // skip university field
.Select(ParsePerson)
.ToArray();
return new University {Name = universityName, Persons = persons};
}
public static Person ParsePerson(string field)
{
var p = field.Split(' ');
return new Person{FirstName = p.First(), LastName = p.Last()};
}
public class University
{
public string Name {get;set;}
public Person[] Persons {get;set;}
}
public class Person
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
我会在这里使用正则表达式
string input = @"**XXX University**: Martha Zander, Rick Anderson; **Albert School**: Nancy Vanderburg, Eric Towson, George Branson; **Hallowed Halls**: Jane Goodall, Ann Crabtree, Rick Grey, Tammy Hudson;";
var dict = Regex.Matches(input, @"**(.+?)**: (.+?);")
.Cast<Match>()
.ToDictionary(x => x.Groups[1].Value, x=>x.Groups[2].Value.Split(','));
dict
将是(在 JSON 中):
{
"XXX University": [
"Martha Zander",
" Rick Anderson"
],
"Albert School": [
"Nancy Vanderburg",
" Eric Towson",
" George Branson"
],
"Hallowed Halls": [
"Jane Goodall",
" Ann Crabtree",
" Rick Grey",
" Tammy Hudson"
]
}