4est
4est

Reputation: 3168

String.Join is not working with AppendAllText (C#)

I have below string

string str= "Insert into " + tname + "(id, t, v) values(" + lc+ ", " + mc+ ", " + rc+");" + Environment.NewLine;

and I'm write it to file:

File.AppendAllText(fileName, str);

It's working.

I also tried to use string.Join:

string str = string.Join("Insert into " + tname+ "(id, t, v) values(" + lc+ ", " + mc+ ", " + rc+ ");", Environment.NewLine);
File.AppendAllText(fileName, str);

but the file always is empty. What is wrong?

Upvotes: 0

Views: 318

Answers (2)

Hamad
Hamad

Reputation: 161

i think what you need is string.Format()

string str = string.Format("Insert into {0}(id, t, v) values({1}, {2}, {3});{4}",tname, lc,mc,rc, Environment.NewLine);

String.Format() documentation

Upvotes: 2

Tim Rutter
Tim Rutter

Reputation: 4679

string.Join is to concatenate a String[] of objects using a separator

eg

List<int> l= new List  { 1,2,3 };
var s = string.Join(",",l);

s is then "1,2,3"

In your code you are basically passing in a very long separator (your string) and an empty array.

Documentation for string.Join

Upvotes: 0

Related Questions