我有这种文字格式:
name:
last name:
birthday:
years old:
parent:
school:
我有下一个信息..
name:name1
last name:lastname1
birthday:13/03/1991
years old:20
parent:fatherx
school:university x
我如何获得:
name1
lastname1
13/03/1991
20
fatherx
university x
...对于不同的变量? 不要忘记用户有时他们没有信息,例如他们有空
parent:
答案 0 :(得分:2)
Split
。例如,如果您将每条线存储在一个单独的字符串中,则可以执行以下操作:
string s = "name:angel rodrigo";
string name= s.Split(':')[1]; // Get everything after the colon
答案 1 :(得分:1)
您可以使用以下代码创建键值对字典。
List<string> fields = new List<string>
{
"name:",
"last name:",
"birthday:",
"years old:",
"parent:",
"school:",
};
string rawData =
@"name:angel rodrigo
last name:uc ku
birthday:13/03/1991
years old:20
parent:fernando uc puc
school:university x";
var data =
fields.ToDictionary(
field => field.TrimEnd (':'),
field => Regex.Match(rawData, "(?<=" + Regex.Escape(field) + ").*"));
foreach (var kvp in data)
{
Console.WriteLine(kvp.Key + " => " + kvp.Value);
}
产生这个结果:
name => angel rodrigo
last name => uc ku
birthday => 13/03/1991
years old => 20
parent => fernando uc puc
school => university x