How to delete last character in a string in C#?

前端 未结 10 1894
一个人的身影
一个人的身影 2020-12-23 00:21

Building a string for post request in the following way,

  var itemsToAdd = sl.SelProds.ToList();
  if (sl.SelProds.Count() != 0)
  {
      foreach (var item         


        
10条回答
  •  囚心锁ツ
    2020-12-23 01:06

    I would just not add it in the first place:

     var sb = new StringBuilder();
    
     bool first = true;
     foreach (var foo in items) {
        if (first)
            first = false;
        else
            sb.Append('&');
    
        // for example:
        var escapedValue = System.Web.HttpUtility.UrlEncode(foo);
    
        sb.Append(key).Append('=').Append(escapedValue);
     }
    
     var s = sb.ToString();
    

提交回复
热议问题