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

前端 未结 10 1993
一个人的身影
一个人的身影 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 00:51

    It's better if you use string.Join.

     class Product
     {
       public int ProductID { get; set; }
     }
     static void Main(string[] args)
     {
       List products = new List()
          {   
             new Product { ProductID = 1 },
             new Product { ProductID = 2 },
             new Product { ProductID = 3 }
          };
       string theURL = string.Join("&", products.Select(p => string.Format("productID={0}", p.ProductID)));
       Console.WriteLine(theURL);
     }
    

提交回复
热议问题