首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >验证文本文件分隔行的面向对象方法

验证文本文件分隔行的面向对象方法
EN

Stack Overflow用户
提问于 2015-09-14 11:24:39
回答 2查看 373关注 0票数 0

我在文本文件中有一个管道分隔行。验证行的最佳方法是什么。我有一个明确的格式,如何在行中的每一个令牌应该是,例如;假设第5项应该是一个日期。

有谁能帮我实现这一目标的最好的面向对象的方法是什么?有什么设计模式可以实现吗?

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-14 11:40:51

您正在寻找用于验证的特定模式。这可以通过多种方式完成,但最简单的方法是在对象的构造函数中进行验证。由于您正在寻找一种更面向对象的方法,您可能会考虑创建一个对象来表示文件,并创建一个对象来表示每个记录。除了联想之外,这里没有真正的模式。但是,您可以利用迭代器模式允许在循环中迭代记录。您说的是读取文本文件,所以这还不够复杂,但如果是这样的话,您可以考虑创建文件对象的工厂模式。如果需要验证的内容很多,那么您可以创建一个单独的方法来验证类中的每个方法。我说的就是一个例子..。

代码语言:javascript
运行
复制
 static void Main(string[] args)
    {

        DataFile myFile = new DataFile(@"C:\...");

        foreach (DataRecord item in myFile)
        {
            Console.WriteLine("ID: {0}, Name: {1}, StartDate: {2}", item.ID, item.Name, item.StartDate);
        }

        Console.ReadLine();
    }


    public class DataFile : IEnumerable<DataRecord>
    {
        HashSet<DataRecord> _items = new HashSet<DataRecord>();


        public DataFile(string filePath)
        {
            // read your file and obtain record data here... 
            //I'm not showing that
            //... then begin iterating through your string results
            //... though I'm only showing one record for this example

            DataRecord record = new DataRecord("1234|11-4-2015|John Doe");
            _items.Add(record);
        }


        public IEnumerator<DataRecord> GetEnumerator()
        {
            foreach (DataRecord item in _items)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public class DataRecord
    {
        private int _id;

        public int ID
        {
            get { return _id; }
            private set { _id = value; }
        }

        private DateTime _startDate;

        public DateTime StartDate
        {
            get { return _startDate; }
            private set { _startDate = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            private set { _name = value; }
        }

        internal DataRecord(string delimitedRecord)
        {
            if (delimitedRecord == null)
                throw new ArgumentNullException("delimitedRecord");

            string[] items = delimitedRecord.Split('|');


            //You could put these in separate methods if there's a lot

            int id = 0;
            if (!int.TryParse(items[0], out id))
                throw new InvalidOperationException("Invalid type...");

            this.ID = id;

            DateTime startDate = DateTime.MinValue;
            if (!DateTime.TryParse(items[1], out startDate))
                throw new InvalidOperationException("Invalid type...");

            this.StartDate = startDate;

            //This one shouldn't need validation since it's already a string and 
            //will probably be taken as-is
            string name = items[2];

            if (string.IsNullOrEmpty(name))
                throw new InvalidOperationException("Invalid type...");

            this.Name = name;
        }


    }
票数 1
EN

Stack Overflow用户

发布于 2015-09-14 12:00:35

要做到这一点,“干净”的方法是使用正则表达式。下面是一个基本的例子:

代码语言:javascript
运行
复制
var allLines = new List<string>();

for (int i = 0; i < 5; i++)
{
     allLines.Add("test" + i);
}

// if you add this line, it will fail because the last line doesn't match the reg ex
allLines.Add("test");

var myRegEx = @"\w*\d"; // <- find the regex that match your lines
Regex regex = new Regex(myRegEx);
var success = allLines.All(line => regex.Match(line).Success);

在本例中,我的regex正在等待一个单词,后面紧跟着一个数字。你所要做的就是找到与你的线条相匹配的正则表达式。

您还可以使用更复杂的reg来避免linq表达式。

把你的钱给我们这样我们就可以帮你了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32563598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档