将值分隔为列

时间:2016-05-06 00:47:30

标签: c#

我想将值分隔为列。 3列是:EmployeeId,Name,Salary。

我不想使用split方法,所以我试试这个

  private static void Main(string[] args)
    {
        List<string> list1= new List<string>();
        List<string> list2= new List<string>();
        List<string> list3= new List<string>();



        var word= "";
        using (var rd = new StreamReader(@"C:\xxx.csv"))
        {
            while (!rd.EndOfStream)
            {

                var line = rd.ReadLine();
                for (int i = 0; i < line.Length; i++)
                {
                    if (line[i] != Convert.ToChar(","))
                    {
                        word= word+ line[i];
                    }
                    if (line[i] == Convert.ToChar(","))
                    {
                        list1.Add(word);
                        word= "";
                    }
                    if (i == (line.Length - 1))
                    {
                        list3.Add(word);
                    }
                }
            }
        }

        Console.WriteLine("Employee ID:");
        foreach (var num in list1)
            Console.WriteLine(num);

        Console.WriteLine("employeename:");
        foreach (var employeename in list2)
            Console.WriteLine(employeename);
        Console.WriteLine("Salary");
        foreach (var employeesalary in list3)
        {
            Console.WriteLine(employeesalary);
        }
        Console.ReadKey();
    }

}
}

正确分配了employeeSalary列,但EmployeeName列接收所有值(错误),而EmployeeID不包含任何内容。 任何人都可以帮我找到错误吗?

3 个答案:

答案 0 :(得分:1)

我知道您要求不使用.Split(...),但为什么不呢?您的代码可能如下所示:

string[][] data =
    File
        .ReadAllLines(@"C:\xxx.csv")
        .Select(line => line.Split(','))
        .ToArray();

List<string> list1 = data.Select(line => line[0]).ToList();
List<string> list2 = data.Select(line => line[1]).ToList();
List<string> list3 = data.Select(line => line[2]).ToList();

答案 1 :(得分:0)

如果我要求使用简单的循环并且不使用任何字符串函数,我可以快速地解决这个问题。

在while循环中使用此逻辑,可能您可能希望将List声明移到之前。

bool firstcolumn= false, secondcolumn = false, thirdcolumn= false;

System.Text.StringBuilder sb = new System.Text.StringBuilder();
List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
for (int i = 0; i < line.Length; i++)
{
    if(line[i] != ',' && i != line.Length -1) 
    {
        sb.Append(line[i]);
        continue;
    }

    if(!firstcolumn)
    {
        firstcolumn = true;
        list1.Add(sb.ToString());

    }
    else if(!secondcolumn)
    {
        secondcolumn = true;
        list2.Add(sb.ToString());

    }
    else
    {
        thirdcolumn = true;
        list3.Add(sb.ToString());
    }

    sb.Clear();     
}

工作Demo

答案 2 :(得分:0)

这是您的代码的工作版本。我添加了一些注释,删除了不必要的Convert.ToChar调用,并在打印周围添加了花括号,就像通用代码清理一样。注意else-if也在循环内,因为某些情况是互斥的。我的答案与我对您原来帖子的评论相符。

    private static void Main(string[] args)
    {
        var employeeIDs = new List<string>();
        var employeeNames = new List<string>();
        var employeeSalaries = new List<string>();


        var tmp = "";
        using (var rd = new StreamReader(@"values.csv"))
        {
            while (!rd.EndOfStream)
            {
                var line = rd.ReadLine();
                bool isFirstColumn = true;          //Indicates we are looking for first column value at this time

                for (int i = 0; i < line.Length; i++)
                {
                    if (line[i] != ',')
                    {
                        tmp = tmp + line[i];
                    }
                    else if (line[i] == ',')
                    {
                        if (isFirstColumn)
                        {
                            //This is the ID, because isFirstColumn is still set to true
                            employeeIDs.Add(tmp);
                            isFirstColumn = false;      //Next "," separates the name
                        }
                        else
                        {
                            //This is the name, because isFirstColumn is now false
                            employeeNames.Add(tmp);
                        }

                        tmp = "";
                    }

                    if (i == (line.Length - 1))
                    {
                        employeeSalaries.Add(tmp);
                        tmp = "";
                    }
                }
            }
        }

        Console.WriteLine("Employee ID:");
        foreach (var num in employeeIDs)
        { 
            Console.WriteLine(num);
        }

        Console.WriteLine("name:");
        foreach (var name in employeeNames)
        {
            Console.WriteLine(name);
        }

        Console.WriteLine("Salary");
        foreach (var salary in employeeSalaries)
        {
            Console.WriteLine(salary);
        }

        Console.ReadKey();
    }