简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM