Add a character on each line of a string

前端 未结 3 850
没有蜡笔的小新
没有蜡笔的小新 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:41

    char []split = new char[]{' '};
    
    //replaces all " " with ";", contiguous "    " will be replaced with a single ";"
    var c2 = String.Join(";", content1.Split(split, StringSplitOptions.RemoveEmptyEntries)); 
    
    //replaces all newlines with a semicolon followed by a newline, thus appends a semicolon to the end of line. 
    var c3 = c2.Replace(System.Environment.NewLine, ";"+System.Environment.NewLine);
    
    //If the file did not end with an NewLine, append a semicolon to the last line
    if (!c3.EndsWith(System.Environment.NewLine)) c3+=";";
    
    File.WriteAllText(path, c3);
    

    It's not the fastest solution, but it works.

提交回复
热议问题