-1

I'm making a CSV converter for this I need to replace all the spaces of my string with a ";". I've tried:

string content = tbxArret.Text;
string path = @"C:\Users\DanyWin\Desktop\CsvOutput\test.csv";
string[] space = new string[] { " " }; 

foreach (string contenu in content.Split(space, StringSplitOptions.RemoveEmptyEntries))
{
    content.Replace(" ", ";");
    File.WriteAllText(path, content);
}

But it didn't work. The text in the CSV file is just the copy of what I put in the content.

Example : a string abc ="a b c "; should return "a;b;c;".

Any help ? :)

drneel
  • 2,887
  • 5
  • 30
  • 48
DanyDC
  • 73
  • 1
  • 14

1 Answers1

6

String.Replace() don't change content it returns value. It should be:

content = content.Replace(" ", ";");
teo van kot
  • 12,350
  • 10
  • 38
  • 70
  • If you want to change content, use `StringBuilder` instead – DavWEB Mar 16 '16 at 14:07
  • 2
    Exactly, operation on a string are immutables, which mean that they never modify the original string, but return a new string. – Pierre-Alain Vigeant Mar 16 '16 at 14:07
  • Ty for your answer, it works. Now I've got another problem : all the spaces are replaced with a ";" but I have a multiline textbox and I need to put a ";" at the end of each line so that my program functions. How can I do that ? Example : Here is my string : string abc ="a b c\nc d e\n"; After the replace : string abc ="a;b;c;\nc;d;e;\n; @teo van kot – DanyDC Mar 16 '16 at 18:01