Fired from Amazon.com
Fired from Amazon.com

Reputation: 265

Proper way in C# to combine an arbitrary number of strings into a single string

I breezed through the documentation for the string class and didn't see any good tools for combining an arbitrary number of strings into a single string. The best procedure I could come up with in my program is

string [] assetUrlPieces = { Server.MapPath("~/assets/"), 
                             "organizationName/",
                             "categoryName/",
                             (Guid.NewGuid().ToString() + "/"),
                             (Path.GetFileNameWithoutExtension(file.FileName) + "/")
                           };

string assetUrl = combinedString(assetUrlPieces);

private string combinedString ( string [] pieces )
{
    string alltogether = "";
    foreach (string thispiece in pieces) alltogether += alltogether + thispiece;
    return alltogether; 
}

but that seems like too much code and too much inefficiency (from the string addition) and awkwardness.

Upvotes: 5

Views: 474

Answers (5)

jt25617
jt25617

Reputation: 21

A simple way to do this with a regular for loop: (since you can use the indices, plus I like these loops better than foreach loops)

   private string combinedString(string[] pieces)
   {
    string alltogether = "";
    for (int index = 0; index <= pieces.Length - 1; index++) {
        if (index != pieces.Length - 1) {
             alltogether += string.Format("{0}/" pieces[index]);
        }
    }
    return alltogether;

Upvotes: 0

juharr
juharr

Reputation: 32276

string.Concat is the most appropriate method for what you want.

var result = string.Concat(pieces);

Unless you want to put delimiters between the individual strings. Then you'd use string.Join

var result = string.Join(",", pieces); // comma delimited result.

Upvotes: 3

DWright
DWright

Reputation: 9500

You want a StringBuilder, I think.

var sb = new StringBuilder(pieces.Count());
foreach(var s in pieces) {
    sb.Append(s);
}
return sb.ToString();

Update

@FiredFromAmazon.com: I think you'll want to go with the string.Concat solution offered by others for

  1. Its sheer simplicity
  2. Higher performance. Under the hood, it uses FillStringChecked, which does pointer copies, whereas string.Join uses StringBuilder. See http://referencesource.microsoft.com/#mscorlib/system/string.cs,1512. (Thank you to @Bas).

Upvotes: 4

Jon Skeet
Jon Skeet

Reputation: 1500785

If you want to insert a separator between values, string.Join is your friend. If you just want to concatenate the strings, then you can use string.Concat:

string assetUrl = string.Concat(assetUrlPieces);

That's marginally simpler (and possibly more efficient, but probably insignificantly) than calling string.Join with an empty separator.

As noted in comments, if you're actually building up the array at the same point in the code that you do the concatenation, and you don't need the array for anything else, just use concatenation directly:

string assetUrl = Server.MapPath("~/assets/") +
    "organizationName/" + 
    "categoryName/" +
    Guid.NewGuid() + "/" +
    Path.GetFileNameWithoutExtension(file.FileName) + "/";

... or potentially use string.Format instead.

Upvotes: 17

dotnetom
dotnetom

Reputation: 24901

I prefer using string.Join:

var result = string.Join("", pieces);

You can read about string.Join on MSDN

Upvotes: 15

Related Questions