你可以使用一个regular expression提取值每个属性从源字符串,像这样:
using System.Text.RegularExpressions;
...
Regex balanceRegex = new Regex("(?<=BALANCE:\\s*)[^\\s]+");
string balance = balanceRegex.Match(source).Value;
这可以在一个功能包裹起来,以搜索任何像这样的命名属性:
private static string GetProperty(string source, string propertyName)
{
string pattern = String.Format("(?<={0}:\\s*)[^\\s]+", propertyName);
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
return regex.Match(source).Value;
}
然后,你可以填写一个Person对象是这样的:
Person person = new Person
{
Balance = GetProperty(source, "Balance"),
Escrow = GetProperty(source, "Escrow Payment"),
Acc = GetProperty(source, "Acc")
};
您可能需要调整规则运算,例如,你有空白内你的属性值,例如ACCOUNT NAME: MR SMITH
正则表达式的方法是相当灵活的,因为它会即使性质或空格数量的顺序改变工作。
您需要能够区分标题(属性名称)中的数据。有没有什么规则可以用来做到这一点?标题总是至少有两个空格吗?或者,你可以假设标题字符串总是完全一样吗? – phoog