2

What is the best way to concatenate string to receive ukr:'Ukraine';rus:'Russia';fr:'France' result?

public class Country
{
    public int IdCountry { get; set; }
    public string Code { get; set; }
    public string Title { get; set; }
}

var lst = new List<Country>();
lst.Add(new Country(){IdCountry = 1, Code = "ukr", Title = "Ukraine"});
lst.Add(new Country() { IdCountry = 2, Code = "rus", Title = "Russia" });
lst.Add(new Country() { IdCountry = 3, Code = "fr", Title = "France" });
string tst = ????
Yara
  • 4,441
  • 6
  • 42
  • 62
  • Is your target pattern really `Code:'Title';` followed by `Code:'Title',` ? Notice the first pair ends in a semicolon, the second pair ends in a comma. Is this intentional? – shanabus Feb 19 '12 at 18:23
  • possible duplicate of [What's the best string concatenation method using C#?](http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c) – Jim Garrison Feb 20 '12 at 06:24
  • shanabus, pattern correcred, thank you – Yara Feb 20 '12 at 15:23

5 Answers5

8

I think something like this would be fairly readable:

string tst = string.Join(";", lst.Select(x=> string.Format("{0}:'{1}'", x.Code, x.Title)));

string.Join() is using a StringBuilder under the hood so this should not be creating unnecessary strings while assembling the result.

Since the parameter to string.Join() is just an IEnumerable (.NET 4 required for this overload) you can also split this out into two lines to further improve readability (in my opinion) without impacting performance:

var countryCodes = lst.Select(x=> string.Format("{0}:'{1}'", x.Code, x.Title));
string test = string.Join(";", countryCodes);
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • 1
    Consider replacing string.Format("...") with string.Concat(x.Code, ":'", x.Title, "'"). – Jason Feb 19 '12 at 17:59
  • 2
    @Jason - I don't agree: `string.Format` uses a `StringBuilder` internally so I don't see the performance gain using `string.Concat()` **and** it is much less readable. – BrokenGlass Feb 19 '12 at 18:06
1

You can override ToString method within Country class to return string.format("{0}:'{1}'", Code, Title) and use string.join to join that list members.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • What about the semi colon between the elements! – Lloyd Feb 19 '12 at 18:04
  • 1
    That would go within the `string.Join`, as demonstrated in BrokenGlass’s answer. The code then becomes really elegant: `string.Join(";", lst)`. The `Join` method would take care of calling `ToString` on each element in the sequence. – Douglas Feb 19 '12 at 18:11
-1

In C# 6.0 you can use string interpolation in order to display formatted dates.

string tst = lst.Aggregate((base, current) => 
    $"{base};{current.Code}:'{current.Title}'");
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
-1

the Enumerable.Aggregate method is pretty good.

var tst = lst.Aggregate((base, current) => 
                  base + ";" + String.Format("{0}:'{1}'", current.Code, current.Title));
zellio
  • 31,308
  • 1
  • 42
  • 61
-2

Slightly efficient way as LINQ tends often to be less efficient then simple foreach or for (in this case) loop.

All depends on what exactly you mean, by saying "most efficient way".

Extension method:

public static string ContriesToString(this List<Country> list)
{
    var result = new StringBuilder();
    for(int i=0; i<list.Count;i++)
       result.Add(string.Format("{0}:'{1}';", list[i].Code, list[i].Title));

    result.ToString();
}

use:

var lst = new List<Country>();
lst.Add(new Country(){IdCountry = 1, Code = "ukr", Title = "Ukraine"});
lst.Add(new Country() { IdCountry = 2, Code = "rus", Title = "Russia" });
lst.Add(new Country() { IdCountry = 3, Code = "fr", Title = "France" });
string tst = lst.ContriesToString();
Tigran
  • 61,654
  • 8
  • 86
  • 123