将列表中的元素从字符串转换为int

时间:2015-07-19 23:17:08

标签: c# .net string data-structures

我正在阅读电子表格中的整数数据,如下所示:

40  50  60  70  80

我可能做错了(这就是我要问的原因)但是我正在将数据作为字符串读取然后在选项卡上拆分,这样我就可以创建一个数组并访问这些元素来进行进一步的计算,但是我最终必须转换回int (int x = Int32.Parse(next);)才能进行任何操作。我知道这一切似乎非常低效。有什么建议可以在这里简化一下吗?

StreamReader stRead = new StreamReader(@"TestData.txt");
{
    while (!stRead.EndOfStream)
    {
        listBox1.Items.Add(stRead.ReadLine());
    }

    string test = (string)listBox1.Items[5];

    string[] words = test.Split('\t');

    string next = words[2];
    int x = Int32.Parse(next);

    int y = x * x;
}

4 个答案:

答案 0 :(得分:1)

我猜你没有真正的性能提升,但你可以摆脱一些不必要的局部变量。

例如:

DataTable dt = new DataTable();
var lines = File.ReadAllLines(pathToFile);
dt.Columns.AddRange(lines.First().Split('\t').Select(col => new DataColumn(){DataType = typeof(Int32)}).ToArray());
lines.Select(x => x.Split('\t')).ToList().ForEach(row => dt.Rows.Add(row));

如果你真的不需要ListBox,DataTable是处理如此大量数据的另一种选择。您可以使用Int32类型创建所需数量的列。

use reporting
db.grantRolesToUser(
    "reportsUser",
    [
      { role: "read", db: "accounts" }
    ]
)

答案 1 :(得分:1)

这可能会更好:

int y =
    File
        .ReadAllLines(@"TestData.txt")
        .Skip(5)
        .Select(x => int.Parse(x.Split('\t')[2]))
        .Select(x => x * x)
        .First();

但我怀疑你需要它更强大。请告诉我们实际数据结构是什么。

答案 2 :(得分:0)

或者您可以使用class Faculty { string firstName, lastName, address, dateOfBirth, phoneNO, fathersName, mothersName; public string FirstName { get { return firstName; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid first name"); else firstName = value; } } public string LastName { get { return lastName; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid last name"); else lastName = value; } } public string Address { get { return this.address; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid adddress"); else address = value; } } public string DateOfBirth { get { return this.dateOfBirth; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid date of birth"); else dateOfBirth = value; } } public string PhoneNo { get { return this.phoneNO; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid phone no."); else phoneNO = value; } } public string FathersName { get { return this.fathersName; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid name"); else fathersName = value; } } public string MothersName { get { return this.mothersName; } set { if (string.IsNullOrEmpty(value)) throw new Exception("Enter a valid name"); else mothersName = value; } } } class ExaminationCentre { Dictionary<int, Faculty> dict = new Dictionary<int, Faculty>(); Faculty counsellor; int id = 100; DateTime yearOfBirth; public void AddMembers() { counsellor = new Faculty(); ; Console.Write("Enter your first name: "); counsellor.FirstName = Console.ReadLine(); Console.Write("Enter your last name: "); counsellor.LastName = Console.ReadLine(); Console.Write("Enter your date of birth: "); counsellor.DateOfBirth = Console.ReadLine(); yearOfBirth = DateTime.Parse(counsellor.DateOfBirth); int age = DateTime.Now.Year - yearOfBirth.Year; Console.Write("Enter your father's name: "); counsellor.FathersName = Console.ReadLine(); Console.Write("Enter your mother's name: "); counsellor.MothersName = Console.ReadLine(); Console.Write("Enter your phone no: "); counsellor.PhoneNo = Console.ReadLine(); showID(); dict.Add(id, counsellor); } void showID() { Console.WriteLine("Your id is: " , id++);//but when I'm doing Console.WriteLine("Your id is:"+id++); it isshowing the id. } } class Program { static void Main(string[] args) { ExaminationCentre centre; Console.WriteLine("Menu"); Console.WriteLine("1.Add"); Console.WriteLine("2.Update"); Console.WriteLine("3.Delete"); Console.WriteLine("4.Search"); int select = int.Parse(Console.ReadLine()); switch (select) { case 1: centre = new ExaminationCentre(); centre.AddMembers(); break; } } } ,我更改了您的初始代码只是为了说明概念

ForEach

答案 3 :(得分:0)

您可以使用传递给它的函数参数的Select方法。

var str = "40\t50\t60\t70\t80";
var ints = str.Split('\t').ToList().Select(int.Parse);