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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:12:23+00:00 2026-05-11T19:12:23+00:00

I have a template string and an array of parameters that come from different

  • 0

I have a template string and an array of parameters that come from different sources but need to be matched up to create a new “filled-in” string:

string templateString = GetTemplate();   // e.g. "Mr {0} has a {1}"
string[] dataItems = GetDataItems();     // e.g. ["Jones", "ceiling cat"}

string resultingString = String.Format(templateString, dataItems);
// e.g. "Mr Jones has a ceiling cat"

With this code, I’m assuming that the number of string format placeholders in the template will equal the number of data items. It’s generally a fair assumption in my case, but I want to be able to produce a resultingString without failing even if the assumption is wrong. I don’t mind if there are empty spaces for missing data.

If there are too many items in dataItems, the String.Format method handles it fine. If there aren’t enough, I get an Exception.

To overcome this, I’m counting the number of placeholders and adding new items to the dataItems array if there aren’t enough.

To count the placeholders, the code I’m working with at the moment is:

private static int CountOccurrences(string haystack)
{
    // Loop through all instances of the string "}".
    int count = 0;
    int i = 0;
    while ((i = text.IndexOf("}", i)) != -1)
    {
        i++;
        count++;
    }
    return count;
}

Obviously this makes the assumption that there aren’t any closing curly braces that aren’t being used for format placeholders. It also just feels wrong. 🙂

Is there a better way to count the string format placeholders in a string?


A number of people have correctly pointed out that the answer I marked as correct won’t work in many circumstances. The main reasons are:

  • Regexes that count the number of placeholders doesn’t account for literal braces ( {{0}} )
  • Counting placeholders doesn’t account for repeated or skipped placeholders (e.g. "{0} has a {1} which also has a {1}")
  • 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-11T19:12:23+00:00Added an answer on May 11, 2026 at 7:12 pm

    Merging Damovisa’s and Joe’s answers.
    I’ve updated answer afer Aydsman’s nad activa’s comments.

    int count = Regex.Matches(templateString, @"(?<!\{)\{([0-9]+).*?\}(?!})")  //select all placeholders - placeholder ID as separate group
                     .Cast<Match>() // cast MatchCollection to IEnumerable<Match>, so we can use Linq
                     .Max(m => int.Parse(m.Groups[1].Value)) + 1; // select maximum value of first group (it's a placegolder ID) converted to int
    

    This approach will work for templates like:

    “{0} aa {2} bb {1}” => count = 3

    “{4} aa {0} bb {0}, {0}” => count = 5

    “{0} {3} , {{7}}” => count = 4

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer One possibility is to wrap TxRx in a mockable class… May 12, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer Your two points are moot: ASP.NET precompilation doesn't result in… May 12, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer No, they're the same. May 12, 2026 at 12:33 am

Related Questions

I'm writing a simple templating layer in PHP but I've got myself a little
I am working on making an expression class: template<typename T, typename U> class expression
Here is one of my header file which consists of a union template with
There's a good discussion of Generics and what they really do behind the scenes

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.