views:

290

answers:

6

Building a string for post request in the following way,

  var itemsToAdd = sl.SelProds.ToList();
  if (sl.SelProds.Count() != 0)
  {
      foreach (var item in itemsToAdd)
      {
        paramstr = paramstr + string.Format("productID={0}&", item.prodID.ToString());
      }
  }

after I get resulting paramstr, I need to delete last character & in it

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

+9  A: 

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();
Marc Gravell
I won't say anything about the readability of this snippet, but this is the way I'd do it.
Matti Virkkunen
@matti - I'm posting from iPod - it makes formatting tricky :)
Marc Gravell
It's also cheap and easy to just shorten the StringBuilder's length by one after the loop is done.
Matt Greer
+1 for best context-specific answer
Steve Townsend
+1 for not adding it instead of removing it, for using a StringBuilder instead of +=, and for chaining appends instead of concatenating and appending. :)
Guffa
+3  A: 
string source;
// source gets initialized
string dest;
if (source.Length > 0)
{
    dest = source.Substring(0, source.Length - 1);
}
Steve Townsend
+1  A: 

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();
Dan Diplo
+8  A: 

Personally I would go with Rob's suggestion, but if you want to remove one (or more) specific trailing character(s) you can use TrimEnd. E.g.

paramstr = paramstr.TrimEnd('&');
Brian Rasmussen
+1 for being the simplest answer for the question's *title*
Rob Fonseca-Ensor
+13  A: 

build it with string.Join instead:

var parameters = sl.SelProds.Select(x=>"productID="+x.prodID).ToArray();
paramstr = string.Join("&", parameters);

string.Join takes a seperator ("&") and and array of strings (parameters), and inserts the seperator between each element of the array.

Rob Fonseca-Ensor
+1 I was just about to write something along those lines.
Brian Rasmussen
+4  A: 

It's better if you use string.Join.

 class Product
 {
   public int ProductID { get; set; }
 }
 static void Main(string[] args)
 {
   List<Product> products = new List<Product>()
      {   
         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);
 }
Danny Chen