Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

an elegant way to build the string in c#

string to build up using keyvaluepair is like this: "name1=v1&name2=v2&name3=v3"

what i am doing:

var sb = new StringBuilder();

foreach (var name in nameValues)
{
            sb.AppendFormat("{0}={1}&", name.Key, name.Value);
 } 

//remove last '&' sign, this is what i think is ugly
sb.ToString().Remove(lastIndex);

any elegant way to avoid the last removal statement of '&' sign?

like image 446
Benny Avatar asked Jan 06 '10 10:01

Benny


4 Answers

var joined =
    String.Join("&", nameValues.Select(n => n.Key + "=" + n.Value).ToArray());

Given that we're not concatenating to one big string (we're producing many small strings) concatenation carries no performace penalties in this case. And in .NET strings are length prefixed anyway so the whole concatenation performance issue is less relevant than in C. String.Join() is very fast as well, faster than StringBuilder.

TLDR: Use String.Join()

like image 161
Tamas Czinege Avatar answered Nov 08 '22 10:11

Tamas Czinege


Take a look here: How to build a query string for a URL in C#?; Quoting:

private string ToQueryString(NameValueCollection nvc)
{
    return "?" + 
        string.Join("&", 
            Array.ConvertAll(
                nvc.AllKeys, 
                key => String.Format("{0}={1}", HttpUtility.UrlEncode(key),
                HttpUtility.UrlEncode(nvc[key]))));
}
like image 29
Rubens Farias Avatar answered Nov 08 '22 10:11

Rubens Farias


foreach (var name in nameValues)
    {
        if (sb.Length > 0) sb.Append("&");
                sb.AppendFormat("{0}={1}", name.Key, name.Value);
     }

Just add "&" when needed, do not remove it from end.

like image 4
Nick43 Avatar answered Nov 08 '22 10:11

Nick43


Here's another approach which I've sometimes used:

var sb = new StringBuilder();
string prefix = "";
foreach (var name in nameValues)
{
    sb.Append(prefix);
    prefix = "&";
    sb.AppendFormat("{0}={1}", name.Key, name.Value);
}

It's just a way of prepending & before every pair other than the first one without using a conditional test.

If you want to use your original idea of trimming the StringBuilder by the way, I'd suggest the following code instead:

sb.Length--; // Remove the last character
return sb.ToString();
like image 3
Jon Skeet Avatar answered Nov 08 '22 08:11

Jon Skeet