A simple C# Q. How to print "{0}"?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I am learning C# and encountered the following problem when I tried to figure out how to print
the string {0} in a Console window
The following piece of codes complied OK. But when I ran it, it failed
The codes are

using System
class Test

public static void Main(

Console.WriteLine("Hello {{0}} {0}", "John"); // Hello {0} Joh
Console.WriteLine("Hello {{0} {0}", "John"); // Erro



Thanks

Ton
 
You need to use String.Format. String.Format("Hello {0}", "John") will
return the string "Hello John".

Console.WriteLine ( String.Format ("Hello {0}", "John") );

Robert
 
I don't understand the original question and I am flabergasted by the
responses.

It sounds like the op is trying to print the string "{0}" to the console
along with some other values.
However he has answered his own question :
Console.WriteLine("Hello {{0}} {0}", "John");
displays :
Hello {0} John
which is exactly what it sounds like he is trying to do.
the second line gives an error , and it should - so don't do it that way.

string.format("text {0}","value of 0") : huh ?
this generates "text value of 0" , how is that printing "{0}" ?

Console.WriteLine ( String.Format ("Hello {0}", "John") ); huh ?
this generates "Hello John" , how is that printing "{0}" ?
also : why bother with string.Format() when Console.WriteLine() has this
formatting capability already.
 
Tony said:
I am learning C# and encountered the following problem when I tried
to figure out how to print the string {0} in a Console window.
The following piece of codes complied OK. But when I ran it, it failed.
The codes are:


using System;
class Test1
{
public static void Main()
{
Console.WriteLine("Hello {{0}} {0}", "John"); // Hello {0} John
Console.WriteLine("Hello {{0} {0}", "John"); // Error
}
}

Basically whenever you need to include a brace, you need to double it
in your format string, so "}}" will print "}", "{{" will print "{" etc.
Another alternative is to use an extra parameter, and make the value of
the parameter the string to print, eg

Console.WriteLine ("Hello {1} {0}", "John", "{0}");
 
It's a replacement code {0} {1} {2} etc... each parameter after the orignal
text string with the codes in it replaces the cordinating number in the
replacement code with that parameter string..

string.format("{0} {1}{3}","Hello,","World","!")

would print "Hello, World!"
 
gerry said:
I don't understand the original question and I am flabergasted by the
responses.

It sounds like the op is trying to print the string "{0}" to the console
along with some other values.
However he has answered his own question :
Console.WriteLine("Hello {{0}} {0}", "John");
displays :
Hello {0} John
which is exactly what it sounds like he is trying to do.
the second line gives an error , and it should - so don't do it that way.

string.format("text {0}","value of 0") : huh ?
this generates "text value of 0" , how is that printing "{0}" ?

Console.WriteLine ( String.Format ("Hello {0}", "John") ); huh ?
this generates "Hello John" , how is that printing "{0}" ?
also : why bother with string.Format() when Console.WriteLine() has this
formatting capability already.


To add a bit to your post, there is a situation where doubling up the
closing brace on a format specifier causes a problem:

Say you want to print out the number 42:

Console.WriteLine("{0}", 42); // prints "42"
Console.WriteLine("{0:N}", 42); // prints "42.00"

No problems so far, but let's say you want to print the number 42
enclosed in braces, so you slap a couple of escaped braces around the
format specifier:

Console.WriteLine("{{{0}}}", 42); // OK, prints "{42}"
Console.WriteLine("{{{0:N}}}", 42); // Oops, prints "{N}"

the format string parser gets confused in the second situation.
Apparently this is 'by design', so to work-around it you have to jump
through a hoop or two. Something like:

Console.WriteLine("{{{0:N}{1}", 42, '}'); // OK, prints "{42.00}"
Console.WriteLine("{{{0}}}", 42.ToString( "N")); // OK, prints "{42.00}"

Brad Abrams discusses this (and I stole the above examples from) here:

http://blogs.msdn.com/brada/archive/2004/01/14/58851.aspx

Of course, all of your comments are spot-on; I just wanted to add a bit
of trivia to this brace-escaping thread, since it caused a bit of
head-scratching on my part when I ran into it.
 
Back
Top