C# string format {} understanding?

Hi, in the following code snippet from MSDN, I'm confused how to derive the meaning of the numbers in the curly braces.  I see this formatting often, but don't truly understand it.  
Can anyone explain how "n" = {0},  "animal" = {1} and {2} = 1
or point me to a link that explains,  thanks!

public class Example
{
   public static void Main()
   {
      String str = "animal";
      String toFind = "n";
      int index = str.IndexOf("n");
      Console.WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index);
   }
}

// The example displays the following output:
//        Found 'n' in 'animal' at position 1
Scarlett72Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
This is the same as the string.Format syntax. Those are positions.

{0} will be replaced by toFind
{1} = str
{2} = index
Éric MoreauSenior .Net ConsultantCommented:
AndyAinscowFreelance programmer / ConsultantCommented:
>>"Found '{0}' in '{1}' at position {2}",
{0} is the contents of the first thing after the comma
{1} is the contents of the second thing after the comma
...
{99} is the contents of the hundredth thing after the comma
....

In your example
toFind is 'n'
str is 'animal'
index is '1'

leading to the result you see
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

AndyAinscowFreelance programmer / ConsultantCommented:
ps.
if instead of
Console.WriteLine("Found '{0}' in '{1}' at position {2}",
                        toFind, str, index);

you had
Console.WriteLine("Found '{2}' in '{1}' at position {0}",
                        index, str, toFind);
you would still get the same result
Scarlett72Author Commented:
Ok, so the reason this formatting is used is to convert whatever is in the variable to a text format ...

I don't understand the ordering, it doesn't seem to matter what order the numbers appear inside the curly braces ...
Eric, how did you know that the numbers inside the braces mapped to those values?
Andy, I don't see the 'comma's' you are referring to ...
Éric MoreauSenior .Net ConsultantCommented:
it refers to the position (0-based) of the arguments you passed to method. The first variable is 0, the second variable is 1, ...
AndyAinscowFreelance programmer / ConsultantCommented:
>>"Found '{0}' in '{1}' at position {2}",
That is a format string inside the " and at the end there is a comma that I referred to

>>I don't understand the ordering, it doesn't seem to matter what order the numbers appear inside the curly braces ...

correct, the order within the string being formatted is immaterial, what is important is the order the variables appear in later on.

repeating part of my earlier comment.
{0} is the contents of the first thing after the comma
{1} is the contents of the second thing after the comma
...
{99} is the contents of the hundredth thing after the comma
....
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  Look at the link eric posted earlier, maybe that will also help you in understanding this.


Note this referring to the arguments by position also means you can repeat things easily.  In the following silly example:
Console.WriteLine("{0} {1}, {0} {1}, {0} {1}", "hello", "world") the output would be:
hello world, hello world, hello world

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scarlett72Author Commented:
Thank you both for being patient, it just clicked!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.