Best way to remove the last character from a string built with stringbuilder

后端 未结 12 2378
-上瘾入骨i
-上瘾入骨i 2020-12-23 14:30

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

12条回答
  •  梦毁少年i
    2020-12-23 14:49

    The most simple way would be to use the Join() method:

    public static void Trail()
    {
        var list = new List { "lala", "lulu", "lele" };
        var data = string.Join(",", list);
    }
    

    If you really need the StringBuilder, trim the end comma after the loop:

    data.ToString().TrimEnd(',');
    

提交回复
热议问题