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

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

    It's good practice to use a StringBuilder when concatenating a lot of strings and you can then use the Remove method to get rid of the final character.

    StringBuilder paramBuilder = new StringBuilder();
    
    foreach (var item in itemsToAdd)
    {
        paramBuilder.AppendFormat(("productID={0}&", item.prodID.ToString());
    }
    
    if (paramBuilder.Length > 1)
        paramBuilder.Remove(paramBuilder.Length-1, 1);
    
    string s = paramBuilder.ToString();
    

提交回复
热议问题