I have a loop that builds up address fields, some of these fields may be empty at the end of the string
List<string> list = new List<string>();
//list can contain any number of values, some of which might be "" (empty string)
string returnValue = "";
for (int iRow = 1; iRow <= list.Count; iRow++)
returnValue += String.Format("{0}, ", list[iRow]);
returnValue = returnValue.Trim();
my output is
asd, aaa, qwe, 123123, , , , ,
How can i remove the trailing ", " from the string?
returnValue = returnValue.TrimEnd(' ', ',');
You should also avoid using strings in your case, instead use StringBuilder. Avoid also using senseles formatting -just list[iRow] is a better option.
Try something like this instead:
string result = string.Join(", ",
list.Where(s => !string.IsNullOrEmpty(s)).ToArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With