codeandcloud
codeandcloud

Reputation: 55248

Joining a List in a different way

Language: C# 4.0

There is a List<string> with values Value1, Value2, Value3 and so on.
I would like to perform a join on the generic List so that the output should be

string values = "Value1, Value2 and Value3"; 

and if there is only one value

string values = "Value1"; 

I am currently replacing LastIndexOf , with and. But did not like that much.
Whats the most efficient method to join like this?

Upvotes: 3

Views: 371

Answers (7)

KilZone
KilZone

Reputation: 1615

You can use the String.Join(string glue, string[] array); function for this.

string values = String.Join(", ", myList.ToArray());

Hope that helps. (Note: myList is the List you already have in your application somewhere.)

Update: Noticed your and requirement a little too late, my appologies, here is a little update:

string values = String.Join(", ", myList.Take(myList.Count - 1)) 
                   + " and " + myList.Last();

Alternatively (or maybe even better) you can use a StringBuilder for this, instead of string concatenation. (Remember: You will still have to check if you have only one value in the list.)

Update: Here is a simple extension method.

public static string SmartJoin(this List<string> items, string lastSeparator)
{
    string values = "";
    if(!items.Any())
    {
        return "";
    }
    if (items.Count > 1)
    {
        values = String.Format("{0} {1} {2}",
                    String.Join(", ", items.Take(items.Count - 1)),
                    lastSeparator,
                    items.Last());
    }
    else
    {
        values = items.First();
    }
    return values;
}

And one can call it like

string values = myList.SmartJoin("and");

Upvotes: 2

Christian
Christian

Reputation: 4342

I personally like to use the methods from the Enumerable-namespace for tasks like this :)

List<string> values = new List<string>() { "Value1", "Value2", "Value3"};
string result = "";
if (values.Count == 1) {
    result = values[0];
}
else if (values.Count > 1 ) {
    var init = values.Take(values.Count - 1);
    var temp = init.Aggregate((acc, x) => acc + ", " + x);
    temp += " and " + values[values.Count -1];
    result = temp;
}

Upvotes: 3

LukeH
LukeH

Reputation: 269558

var sb = new StringBuilder();
if (yourList.Count > 0)
{
    sb.Append(yourList[0]);

    if (yourList.Count > 1)
    {
        for (int i = 1; i < yourList.Count - 1; i++)
        {
            sb.Append(", ").Append(yourList[i]);
        }

        sb.Append(" and ").Append(yourList[yourList.Count - 1]);
    }
}
string values = sb.ToString();

Upvotes: 1

Stefan
Stefan

Reputation: 14880

StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
  if (i == sb.Count - 1)
  {
    sb.Append(" and ");
  }
  else if (i > 0)
  {
    sb.Append(", ");
  }
  sb.Append(list[i])
}

Upvotes: 4

Ahmed Magdy
Ahmed Magdy

Reputation: 6040

The method:

private static string ConvertToString(List<string> list)
{
    if (!list.Any())
    {
        return string.Empty;
    }

    if (list.Count == 1)
    {
        return list.First();
    }

    return string.Join(", ", list.Take(list.Count - 1)) + " and " + list.Last();
}

examples

var list1 = new List<string> { "Value1", "Value2", "Value3", "Value4" };
var list2 = new List<string> { "Value1", "Value2", "Value3" };
var list3 = new List<string> { "Value1", "Value2" };
var list4 = new List<string> { "Value1" };
Console.WriteLine(ConvertToString(list1));
Console.WriteLine(ConvertToString(list2));
Console.WriteLine(ConvertToString(list3));
Console.WriteLine(ConvertToString(list4));

NOTE: I'm using .net 4 code

UPDATE: removed unnecessary code UPDATE2: did some code optimization used some tips form @KilZone

Upvotes: 1

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234654

The no-nonsense answer:

var sb = new StringBuilder();
for(int i = 0; i < items.Count; i++)
{
    if(i != 0)
    {
        if(i == items.Count-1) sb.Append(" and ");
        else sb.Append(", ");
    }
    sb.Append(items[i]);
}
return sb.ToString();

If you want to perform an operation that has a couple of special cases (no leading or trailing commas, and the last comma is actually " and "), the simplest way is to just write special cases.

Upvotes: 2

Andrew Orsich
Andrew Orsich

Reputation: 53695

You can join using function like this:

 public string SmartJoin(string lastSeparator, List<string> array)
 {
   var lastElementPosition = array.Count - 1;
   var lastValue = array[lastElementPosition];
   array.RemoveAt(lastElementPosition);

   return String.Join(lastSeparator, new[] { String.Join(", ", array), lastValue });
 }

Example:

   var items = new List<string> { "Value1", "Value2", "Value3" };
   var result =  SmartJoin(" and ", items);

Upvotes: 1

Related Questions