如何以正确的方式使用String.skip函数?

时间:2019-01-07 07:18:48

标签: c# asp.net

我有一个文本文件,包含大约8000行。 每行必须包含90个字符。 每行包含不同的块,因此我必须为每个块使用子字符串。 例如 xx-xxxxxxxxx-xx-xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxx

我使用Substring(x,x)在for循环中获取每个块。

有些行没有122个字符,

例如xx-xxxxxxxxx-xx-xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxx-xxxxx

所以我想跳过那些行,并继续讨论其他内容。我不了解C#中String.Skip函数的正确用法。

这是我的代码。

public void getext()
{

    // bool check = false;
    MySqlConnectionStringBuilder con = new MySqlConnectionStringBuilder();
    con.Server = "xxx";
    con.UserID = "xxx";
    con.Password = "xxx";
    con.Database = "xxx";
    MySqlConnection sqlcon = new MySqlConnection(con.ToString());
    string padding = "";

    try
    {
        sqlcon.Open();
        //var watch = System.Diagnostics.Stopwatch.StartNew();
        var path = "C:\\xxx\\xxx\\xxx\\1.txt";
        var lines = File.ReadAllLines(path);
        for (var i = 0; i < lines.Length; i += 1)
        {
            string str = lines[i].ToString();
            string code, control1, actype, control2, filler, control3, sponsor,
            control4, amtdate, control5;                
            code = str.Substring(0, 2);
            control1 = str.Substring(2, 9);
            actype = str.Substring(11, 2);
            control2 = str.Substring(13, 18);
            filler = str.Substring(31, 40);
            control3 = str.Substring(74, 16);
            sponsor = str.Substring(90, 20);                
            MySqlCommand cmd = new MySqlCommand("addext", sqlcon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("code", MySqlDbType.VarChar).Value = code;
            cmd.Parameters.Add("control1", MySqlDbType.VarChar).Value = control1;
            cmd.Parameters.Add("actype", MySqlDbType.VarChar).Value = actype;
            cmd.Parameters.Add("control2", MySqlDbType.VarChar).Value = control2;
            cmd.Parameters.Add("filler", MySqlDbType.VarChar).Value = filler;
            cmd.Parameters.Add("control3", MySqlDbType.VarChar).Value = control3;
            cmd.Parameters.Add("sponsor", MySqlDbType.VarChar).Value = sponsor;                
            cmd.ExecuteNonQuery();
            //sqlcon.Close();
            label1.Visible = true;
            //sqlcon.Close();
            //watch.Stop();
            //var elapsedMs = watch.Elapsed.TotalMinutes;
            label1.Text = "success";
            // sqlcon.Close();

        }
        sqlcon.Close();
    }
    catch (Exception ex)
    {
        label1.Visible = true;
        label1.Text = "fail" + ex.ToString();
    }

}

2 个答案:

答案 0 :(得分:2)

如果要在循环中跳过数字/行,可以使用

continue;

示例:

for (var i = 0; i < lines.Length; i += 1)
{
    string str = lines[i];

    if (str.length != 122) // We only want to Work with lines which are 122 chars long.
        continue;

    DoWork(); // Insert you code here ;)
}

答案 1 :(得分:0)

我相信您想跳过少于122个字符的行吗?您可以将它们从循环中过滤掉,并仅处理具有所需长度的循环。

foreach(var str in lines.Where(x=>x.Length>=122))
{
string code, control1, actype, control2, filler, control3, sponsor,
            control4, amtdate, control5;                
code = str.Substring(0, 2);

// rest of your code
}

Filter and Foreach方法将避免使用索引,从而提高了代码的可读性