Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5990579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:15:51+00:00 2026-05-22T23:15:51+00:00

Language: C# 4.0 There is a List<string> with values Value1, Value2, Value3 and so

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:15:52+00:00Added an answer on May 22, 2026 at 11:15 pm

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I were looking to create my own language are there any tools that
Is there some rare language construct I haven't encountered (like the few I've learned
Is there a programming language suitable for building web applications, that is compiled, strongly-typed,
Are there any good language-agnostic distributed make systems for linux that are secure and
Basically what I mean is like this: List<String[]> routes = (List<String[]>)application.getAttribute(routes); For the above
Is there any way to get a List<string> which contains all 'usings' within a
If there are any language designers out there (or people simply in the know),
Is there a language agnostic algorithm to name tuples? Specifically, I want the following
According to the Java Language Specification : If there are any enclosing try statements
Is there a structured language for declaring styles in a sensible way, which can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.