I have the following
data.AppendFormat(\"{0},\",dataToAppend);
The problem with this is that I am using it in a loop and there will be a t
How About this..
string str = "The quick brown fox jumps over the lazy dog,";
StringBuilder sb = new StringBuilder(str);
sb.Remove(str.Length - 1, 1);
I recommend, you change your loop algorithm:
You should use the string.Join
method to turn a collection of items into a comma delimited string. It will ensure that there is no leading or trailing comma, as well as ensure the string is constructed efficiently (without unnecessary intermediate strings).
I prefer manipulating the length of the stringbuilder:
data.Length = data.Length - 1;
Yes, convert it to a string once the loop is done:
String str = data.ToString().TrimEnd(',');
Use the following after the loop.
.TrimEnd(',')
or simply change to
string commaSeparatedList = input.Aggregate((a, x) => a + ", " + x)