C# 在列表对象中查找制表符时遇到问题

C# 在列表对象中查找制表符时遇到问题,c#,winforms,C#,Winforms,我在winforms应用程序C#中工作 我正在读取一个文本文件,其中每一行都有由制表符划分的字段: 我将每一行放入名为tic\u string的列表中。从这里,我试图搜索每个列表对象,找到选项卡,并将每个字段放入其自己的数组中。因此,列a,列b,列c将有一个数组。。。等等 问题是,当我试图在列表对象中查找选项卡时,它什么也找不到。这是我的密码: string[] tic_num = new string[row_counter]; string[] tic_title = new string[

我在winforms应用程序C#中工作

我正在读取一个文本文件,其中每一行都有由制表符划分的字段:

我将每一行放入名为
tic\u string
的列表中。从这里,我试图搜索每个列表对象,找到选项卡,并将每个字段放入其自己的数组中。因此,
列a
列b
列c
将有一个数组。。。等等

问题是,当我试图在列表对象中查找选项卡时,它什么也找不到。这是我的密码:

string[] tic_num = new string[row_counter];
string[] tic_title = new string[row_counter];
string[] tic_owner = new string[row_counter];
string[] tic_open_date = new string[row_counter];

int last_tab = 0;
int char_counter = 0;
int feild_counter = 1;
int feild_char_count = 1;
int current_row=0;
string temp_feild = "";
char temp_char;
char tab_char = '\t';
foreach (string tic_string_value in tic_string)
{
    temp_char = tic_string_value[char_counter];
    if (temp_char == tab_char)
    {
        Console.WriteLine("tab_found");

        if (feild_char_count == 1)
        {
            temp_feild = "";
        }
        else
        {
            temp_feild = tic_string_value.Substring(last_tab, feild_char_count);
        }
        last_tab = char_counter;
        feild_char_count = 0;

        switch (feild_counter)
        {
            case 1:
                tic_num[current_row] = temp_feild;
                break;
            case 2:
                tic_title[current_row] = temp_feild;
                break;
            case 3:
                tic_owner[current_row] = temp_feild;
                break;
            case 4:
                tic_open_date[current_row] = temp_feild;
                break;
        }
    }
    current_row++;
    feild_char_count++;
    char_counter++;
    if (feild_counter == 5)
        feild_counter = 1;
}

对于这样简单的任务,您的代码似乎太复杂了。不要逐个字符分析每行字符,只需使用辅助函数,如
String.Split
等:

foreach (string tic_string_value in tic_string)
{
    var parts = tic_string_value.Split(new [] { '\t' }, 
                                       StringSplitOptions.RemoveEmptyEntries);
    tic_num[current_row] = parts[0];
    tic_title[current_row] = parts[1];
    tic_owner[current_row] = parts[2];
    tic_open_date[current_row] = parts[3];
    current_row++;
}

对于这样简单的任务,您的代码似乎太复杂了。不要逐个字符分析每行字符,只需使用辅助函数,如
String.Split
等:

foreach (string tic_string_value in tic_string)
{
    var parts = tic_string_value.Split(new [] { '\t' }, 
                                       StringSplitOptions.RemoveEmptyEntries);
    tic_num[current_row] = parts[0];
    tic_title[current_row] = parts[1];
    tic_owner[current_row] = parts[2];
    tic_open_date[current_row] = parts[3];
    current_row++;
}

对于这样简单的任务,您的代码似乎太复杂了。不要逐个字符分析每行字符,只需使用辅助函数,如
String.Split
等:

foreach (string tic_string_value in tic_string)
{
    var parts = tic_string_value.Split(new [] { '\t' }, 
                                       StringSplitOptions.RemoveEmptyEntries);
    tic_num[current_row] = parts[0];
    tic_title[current_row] = parts[1];
    tic_owner[current_row] = parts[2];
    tic_open_date[current_row] = parts[3];
    current_row++;
}

对于这样简单的任务,您的代码似乎太复杂了。不要逐个字符分析每行字符,只需使用辅助函数,如
String.Split
等:

foreach (string tic_string_value in tic_string)
{
    var parts = tic_string_value.Split(new [] { '\t' }, 
                                       StringSplitOptions.RemoveEmptyEntries);
    tic_num[current_row] = parts[0];
    tic_title[current_row] = parts[1];
    tic_owner[current_row] = parts[2];
    tic_open_date[current_row] = parts[3];
    current_row++;
}

您还可以使用列表而不是4个字符串数组:

    public class ObjectToBeUsed
{
    public Person(int num, string title, string owner, string opendate)
    {
        this.Num = num;
        this.Title = title;
        this.Owner = owner;
        this.OpenDate = opendate;
    }

    private int _num;

    public int Num
    {
        get { return _num; }
        set { _num = value; }
    }


    private string _title;

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _owner;

    public string Owner
    {
        get { return _owner; }
        set { _owner = value; }
    }

    private string _opendate;

    public string OpenDate
    {
        get { return _opendate; }
        set { _opendate = value; }
    }

}
这个类描述文本文件中的每一行

            System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
        string currentLine = null;
        List<ObjectToBeUsed> peopleList = new List<ObjectToBeUsed>();

        while ((currentLine = file.ReadLine()) != null)
        {
            string[] tokens = Regex.Split(currentLine, @"\t");
            peopleList.Add(new ObjectToBeUsed(Convert.ToInt32(tokens[0]), tokens[1], tokens[2], tokens[3]));
        }
System.IO.StreamReader file=new System.IO.StreamReader(“test.txt”);
字符串currentLine=null;
List peopleList=新列表();
而((currentLine=file.ReadLine())!=null)
{
string[]tokens=Regex.Split(currentLine,@“\t”);
添加(新的ObjectToBeUsed(Convert.ToInt32(令牌[0])、令牌[1]、令牌[2]、令牌[3]);
}

代码非常简单,但如果需要进一步解释,请继续。

您也可以使用列表而不是4个字符串数组:

    public class ObjectToBeUsed
{
    public Person(int num, string title, string owner, string opendate)
    {
        this.Num = num;
        this.Title = title;
        this.Owner = owner;
        this.OpenDate = opendate;
    }

    private int _num;

    public int Num
    {
        get { return _num; }
        set { _num = value; }
    }


    private string _title;

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _owner;

    public string Owner
    {
        get { return _owner; }
        set { _owner = value; }
    }

    private string _opendate;

    public string OpenDate
    {
        get { return _opendate; }
        set { _opendate = value; }
    }

}
这个类描述文本文件中的每一行

            System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
        string currentLine = null;
        List<ObjectToBeUsed> peopleList = new List<ObjectToBeUsed>();

        while ((currentLine = file.ReadLine()) != null)
        {
            string[] tokens = Regex.Split(currentLine, @"\t");
            peopleList.Add(new ObjectToBeUsed(Convert.ToInt32(tokens[0]), tokens[1], tokens[2], tokens[3]));
        }
System.IO.StreamReader file=new System.IO.StreamReader(“test.txt”);
字符串currentLine=null;
List peopleList=新列表();
而((currentLine=file.ReadLine())!=null)
{
string[]tokens=Regex.Split(currentLine,@“\t”);
添加(新的ObjectToBeUsed(Convert.ToInt32(令牌[0])、令牌[1]、令牌[2]、令牌[3]);
}

代码非常简单,但如果需要进一步解释,请继续。

您也可以使用列表而不是4个字符串数组:

    public class ObjectToBeUsed
{
    public Person(int num, string title, string owner, string opendate)
    {
        this.Num = num;
        this.Title = title;
        this.Owner = owner;
        this.OpenDate = opendate;
    }

    private int _num;

    public int Num
    {
        get { return _num; }
        set { _num = value; }
    }


    private string _title;

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _owner;

    public string Owner
    {
        get { return _owner; }
        set { _owner = value; }
    }

    private string _opendate;

    public string OpenDate
    {
        get { return _opendate; }
        set { _opendate = value; }
    }

}
这个类描述文本文件中的每一行

            System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
        string currentLine = null;
        List<ObjectToBeUsed> peopleList = new List<ObjectToBeUsed>();

        while ((currentLine = file.ReadLine()) != null)
        {
            string[] tokens = Regex.Split(currentLine, @"\t");
            peopleList.Add(new ObjectToBeUsed(Convert.ToInt32(tokens[0]), tokens[1], tokens[2], tokens[3]));
        }
System.IO.StreamReader file=new System.IO.StreamReader(“test.txt”);
字符串currentLine=null;
List peopleList=新列表();
而((currentLine=file.ReadLine())!=null)
{
string[]tokens=Regex.Split(currentLine,@“\t”);
添加(新的ObjectToBeUsed(Convert.ToInt32(令牌[0])、令牌[1]、令牌[2]、令牌[3]);
}

代码非常简单,但如果需要进一步解释,请继续。

您也可以使用列表而不是4个字符串数组:

    public class ObjectToBeUsed
{
    public Person(int num, string title, string owner, string opendate)
    {
        this.Num = num;
        this.Title = title;
        this.Owner = owner;
        this.OpenDate = opendate;
    }

    private int _num;

    public int Num
    {
        get { return _num; }
        set { _num = value; }
    }


    private string _title;

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _owner;

    public string Owner
    {
        get { return _owner; }
        set { _owner = value; }
    }

    private string _opendate;

    public string OpenDate
    {
        get { return _opendate; }
        set { _opendate = value; }
    }

}
这个类描述文本文件中的每一行

            System.IO.StreamReader file = new System.IO.StreamReader("test.txt");
        string currentLine = null;
        List<ObjectToBeUsed> peopleList = new List<ObjectToBeUsed>();

        while ((currentLine = file.ReadLine()) != null)
        {
            string[] tokens = Regex.Split(currentLine, @"\t");
            peopleList.Add(new ObjectToBeUsed(Convert.ToInt32(tokens[0]), tokens[1], tokens[2], tokens[3]));
        }
System.IO.StreamReader file=new System.IO.StreamReader(“test.txt”);
字符串currentLine=null;
List peopleList=新列表();
而((currentLine=file.ReadLine())!=null)
{
string[]tokens=Regex.Split(currentLine,@“\t”);
添加(新的ObjectToBeUsed(Convert.ToInt32(令牌[0])、令牌[1]、令牌[2]、令牌[3]);
}

这段代码很容易解释,但如果您需要进一步解释,请继续。

首先,我从您的代码风格推断,您可能熟悉C/C++并且是C#新手,因为这段代码有一种特别的“C++”味道。这让我想起了我自己第一次跳转时的C代码

我很高兴您描述了您试图解决的问题,而不是简单地发布代码并询问哪里可以找到错误,因为我认为您实际上可以更简单地解决您的问题

考虑以下代码(假设您正在迭代代码之外的每一行,我省略了一些您已经指定的变量声明):

这充分利用了C语言的简洁性,并删除了相当多的代码行,这总是很好的。Split方法将为您处理大部分字符串拆分,因此无需手动完成所有操作并跟踪字符

注意:我保留了一些字段名的原始命名,尽管通常最好在C#代码中使用

现在,我从您的原始代码中注意到,您的数据中可能没有实际意义上的“行”(即按换行符拆分),但您可能将数据完全分开,并使用每行具有固定数量的列来拆分行的事实

如果是这种情况,我是否可以建议以下代码块可以帮助您:

int i = 0;
        foreach (var group in tic_string.GroupBy(x => i++ % 4)) {
            int current_row = 0;
            foreach (var field in group) {
                switch (group.Key) {
                    case 0:
                        tic_num[current_row] = field;
                        break;
                    case 1:
                        tic_title[current_row] = field;
                        break;
                    case 2:
                        tic_owner[current_row] = field;
                        break;
                    case 3:
                        tic_open_date[current_row] = field;
                        break;
                }
                current_row++;
            }
        }
当然,现在您可能需要根据代码调整这些块,而不是逐字使用。我希望他们至少展示出一种不同的思考问题的方式。特别是,学习使用各种扩展方法和LINQ查询也将非常有帮助——它们是使C#代码能够快速且易于开发的一部分


祝你好运,解决你的问题

首先,我从您的代码风格推断,您可能熟悉C/C++并且是C#的新手,因为这段代码有一种特别的“C++”味道。这让我想起了我自己第一次跳转时的C代码

我很高兴您描述了您试图解决的问题,而不是简单地发布代码并询问去哪里