1

I have a C# List that I want to create a comma separate string. I've found other answers on SO that deal with this, but my particular case I want to only use a portion of the values in the List to create the string.

If my List contained these values:

"Foo" "Bar" "Car"

and I wanted to create a string

Foo, Bar and Car.

I could use this code:

string.Format("{0} and {1}.", 
              string.Join(", ", myList.Take(myList.Count - 1)), 
              myList.Last());

However, my list is actual formed of jSON values like so

{ Name = "Foo" }
{ Name = "Bar" }
{ Name = "Car" }

So the above code results in:

{ Name = "Foo" }, { Name = "Bar" } and { Name = "Car" }.

How would I construct the string such that I only use the Foo, Bar and Car values in the list?

Update

Thanks to @StevePy, this is what I ended up with:

string.Format("{0} and {1}.", 
              string.Join(", ", myList.Select(x => x.Name).ToList().Take(myList.Count - 1)), 
              myList.Select(x => x.Name).ToList().Last());
4
  • 1
    Is your list a List<string> and each string is of the type "{ Name = "Foo" }" ? Commented Dec 20, 2012 at 6:10
  • so what's the actual output that you need? will you include and to your json response? Commented Dec 20, 2012 at 6:12
  • You might want to look at this: stackoverflow.com/questions/5409890/… Commented Dec 20, 2012 at 6:12
  • 1
    You can optimize that solution a bit for the last item by using myList.Last().Name to avoid re-listing the items. Or probably a bit better: myList[myList.Count -1].Name Not as suscinct, but I think .Last() still iterates over the entire set. Commented Dec 20, 2012 at 13:33

5 Answers 5

2

If you need to operate with strings, just grab the necessary part of each string with, for example, String.IndexOf and String.LastIndexOf methods:

List<string> myList = new List<string> { 
    "{ Name = \"Foo\" }",
    "{ Name = \"Bar\" }",
    "{ Name = \"Car\" }"
};

var temp = myList.Select(x =>
    {
        int index = x.IndexOf("\"") + 1;
        return x.Substring(index, x.LastIndexOf("\"") - index);
    })
    .ToList();

string result = string.Format("{0} and {1}.",
                              string.Join(", ", temp.Take(myList.Count - 1)),
                              temp.Last());
Sign up to request clarification or add additional context in comments.

Comments

1

Linq should help.

var nameList = myList.Select(x=>x.Name).ToList();

Comments

0

you can use JsonConvert.toString to get the value of your list item, or if you used a json serialization, you could use the JsonConvert.Deserialization

1 Comment

You could probably provide some code example for this to worth being an answer?
0

I built a method that will do this for you:

static string ConvertToMyStyle(List<string> input)
{
    string result = "";

    foreach(string item in input)
    {
        if(input.IndexOf(item) != input.ToArray().Length-1)
            result += item + ", ";
        else
            result += "and " + item + ".";
    }
    return result;
}

Comments

0

this handles the single item case

protected string FormatWithOxfordCommas(List<string> reasons)
        {
            string result = "";
            if (reasons.Count == 1)
                result += reasons[0];
            else
            {
                foreach (string item in reasons)
                {
                    if (reasons.IndexOf(item) != reasons.Count - 1)
                        result += item + ", ";
                    else
                        result += "and " + item + ".";
                }
            }
            return result;
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.