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 8588435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:48:07+00:00 2026-06-11T22:48:07+00:00

I know several questions have been asked and answered about how to create a

  • 0

I know several questions have been asked and answered about how to create a comma delimited string from a list. I’m looking for help on something slightly different.

What I would like to do is create a display-friendly human-readable string from a List<string> that reads like “A, B and C are invalid values”. The grammar and format of the string should change based on the number of items in the list. The list could contain any number of items.

For example:

List<string> myString = new List<string>() { "Scooby", "Dooby", "Doo" };
// Should return "Scooby, Dooby and Doo are invalid values."

List<string> myString = new List<string>() { "Scooby", "Dooby" };
// Should return "Scooby and Dooby are invalid values."

List<string> myString = new List<string>() { "Scooby" };
// Should return "Scooby is an invalid value."

Here’s what I’ve done so far:

string userMessage = "";
foreach(string invalidValue in invalidValues)
{
  userMessage = " " + userMessage + invalidValue + ",";
}

// Remove the trailing comma
userMessage = userMessage.Substring(0, userMessage.LastIndexOf(','));

if (invalidValues.Count > 1)
{
  int lastCommaLocation = userMessage.LastIndexOf(',');
  userMessage = userMessage.Substring(0, lastCommaLocation) + " and " + userMessage.Substring(lastCommaLocation + 1) + " are invalid values.";
}
else 
{
  userMessage = userMessage + " is an invalid value.";
}

Is there a better or more efficient way to do 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-06-11T22:48:08+00:00Added an answer on June 11, 2026 at 10:48 pm
    public static string FormatList(List<string> invalidItems)
    {
        if(invalidItems.Count == 0) return string.Empty;
        else if(invalidItems.Count == 1) return string.Format("{0} is an invalid value", invalidItems[0]);
        else return string.Format("{0} and {1} are invalid values", string.Join(", ", invalidItems.Take(invalidItems.Count - 1)), invalidItems.Last());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm aware that questions about the stack vs. the heap have been asked several
I know questions about thumbnails have been asked in the past but I can't
I know that this question have been asked several times. But I can't get
I know this question would have been asked several time but still I am
I know tat similar questions has been asked already several times. And i do
I know there has been several questions regarding this very question and I have
I know this question has been asked several times for sure, but I have
I know this Question has been asked several times before, and I have tried
I've read several different questions from SO about this already but still haven't been
I know there have been several questions in the past regarding things that compile

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.