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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:47:23+00:00 2026-05-13T14:47:23+00:00

Before using String.Format to format a string in C#, I would like to know

  • 0

Before using String.Format to format a string in C#, I would like to know how many parameters does that string accept?

For eg. if the string was “{0} is not the same as {1}”, I would like to know that this string accepts two parameters
For eg. if the string was “{0} is not the same as {1} and {2}”, the string accepts 3 parameters

How can I find this efficiently?

  • 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-13T14:47:23+00:00Added an answer on May 13, 2026 at 2:47 pm

    String.Format receives a string argument with format value, and an params object[] array, which can deal with an arbitrary large value items.

    For every object value, it’s .ToString() method will be called to resolve that format pattern

    EDIT: Seems I misread your question. If you want to know how many arguments are required to your format, you can discover that by using a regular expression:

    string pattern = "{0} {1:00} {{2}}, Failure: {0}{{{1}}}, Failure: {0} ({0})";
    int count = Regex.Matches(Regex.Replace(pattern, 
        @"(\{{2}|\}{2})", ""), // removes escaped curly brackets
        @"\{\d+(?:\:?[^}]*)\}").Count; // returns 6
    

    As Benjamin noted in comments, maybe you do need to know number of different references. If you don’t using Linq, here you go:

    int count = Regex.Matches(Regex.Replace(pattern, 
        @"(\{{2}|\}{2})", ""), // removes escaped curly brackets
        @"\{(\d+)(?:\:?[^}]*)\}").OfType<Match>()
        .SelectMany(match => match.Groups.OfType<Group>().Skip(1))
        .Select(index => Int32.Parse(index.Value))
        .Max() + 1; // returns 2
    

    This also address @280Z28 last problem spotted.

    Edit by 280Z28: This will not validate the input, but for any valid input will give the correct answer:

    int count2 =
        Regex.Matches(
            pattern.Replace("{{", string.Empty),
            @"\{(\d+)")
        .OfType<Match>()
        .Select(match => int.Parse(match.Groups[1].Value))
        .Union(Enumerable.Repeat(-1, 1))
        .Max() + 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 381k
  • Answers 381k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I agree with Marvin, delete the old files and generate… May 14, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer The name of a typedef does not represent the syntactical… May 14, 2026 at 10:17 pm
  • Editorial Team
    Editorial Team added an answer $str =~ s/\b([A-Z])\s+(?=[A-Z]\b)/$1/g; May 14, 2026 at 10:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.