我有一个包含管道分隔格式的内容的文件。这是WinForm应用程序中的C#。
完美格式的示例:
1000|2014|01|AP|1|00000001|00
收到的典型格式的示例:
1000|2014|1|AP|1|1
请注意,典型格式不包含第7个值。在这些情况下,应将其默认设置为“ 00”。其他字段也不用前导零填充。这是我的方法。
//string buildcontentfromfile = the contents of each file that I receive and read
char[] delimiter = new char[] {'|'};
string[] contents = buildcontentfromfile.Split(delimiter);
if(contents[0].Length == 4)
{
if(contents[1].Length == 4)
{
if(contents[2].Length == 2)
{
if(contents[3].Length == 2)
{
if(contents[4].Length == 1)
{
if(contents[5].Length == 8)
{
if(contents[6].Length == 2)
{
}
}
}
}
}
}
}
这将解决“完美格式”问题,当然,我将需要添加更多逻辑以解决如何接收它们的“典型格式”,例如检查第7个值,并在需要的字段中添加前导0。他们,和长度。但是我什至以正确的方式走这条路吗?有更简单的方法可以做到这一点吗?谢谢!
使用正则表达式:
var re = new Regex("\d{4}\|\d{4}\|\d\d\|\w\w\|\d\|\d{8}\|\d\d");
var valid = re.IsMatch(input);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句