Add a character on each line of a string

前端 未结 3 793
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 08:47

I make a CSV converter, for this, I need to replace all the spaces with \";\". I have already did this step. The problem is that I have a texbox with the multiline mod. Here is

3条回答
  •  长情又很酷
    2021-01-26 09:45

    content1 seems to contain the whole file.

    So if you want to add semicolons to each line, you could replace the newline with a semicolon and a newline.

    content1 = content1.Replace("\n", ";\n");
    

    You can make your code a bit easier:

    string nom = tbxNom.Text;
    #region Normal
    try
    {
        string content1 = tbxArret.Text;
        string path1 = @"C:\Users\DanyWin\Desktop\CsvOutput\" + nom + ".csv";
        var lines = content1.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(line => Regex.Replace(line, @"\s+", ";") + ";");
        content1 = String.Join("\n", lines);
        File.WriteAllText(path1, content1);
    }
    catch
    {
        lblInfo.Text = "Erreur";
    }
    

提交回复
热议问题