Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing scope ({) into string in console app

I am trying to write a code generator using a c# console application. Now when I type this, I receive an error:

    Console.WriteLine("sphere {{0}{1} {2} texture{Gold_Metal}}", 
    pre, i.ToString(), sprad.ToString());

It says "input in wrong format" I have checked that all the variables were strings, and they are. When I tried

    Console.WriteLine("sphere {0}{1} {2} textureGold_Metal", 
    pre, i.ToString(), sprad.ToString());

It worked perfectly fine. Is there a way to fix this?

like image 497
SharkRetriever Avatar asked Dec 19 '11 01:12

SharkRetriever


People also ask

What is the syntax to print a text in the console?

Console. WriteLine("This is C#"); In this code line, we print the "This is C#" string to the console. To print a message to the console, we use the WriteLine method of the Console class.

How do you write in console?

Writing in Console App In C# you can write or print to console using Console. WriteLine() or Console. Write(), basically both methods are used to print output of console. Only difference between Console.

What is console ReadLine ()?

The ReadLine method reads a line from the standard input stream. (For the definition of a line, see the paragraph after the following list.) This means that: If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.

What is the difference between console WriteLine () and console ReadLine ()?

The only difference between the Read() and ReadLine() is that Console. Read is used to read only single character from the standard output device, while Console. ReadLine is used to read a line or string from the standard output device. Program 1: Example of Console.


2 Answers

Assuming you want a literal { inserted into the stream, you need to escape a your { with yet another brace thus:

Console.WriteLine("sphere {{{0}{1} {2} ...
                          ^^
                          ||-- see here.

Similarly for the end-brace, this is detailed in the MSDN string formatting FAQ here. The sequence {{ becomes { and }} becomes }.

From what I understand your intent to be, the full statement in your specific case would be:

Console.WriteLine("sphere {{{0}{1} {2} texture{{Gold_Metal}}}}", 
    pre, i.ToString(), sprad.ToString());

which should give you something like:

sphere {<Arg0><Arg1> <Arg2> texture{Gold_Metal}}
like image 62
paxdiablo Avatar answered Oct 13 '22 00:10

paxdiablo


You need to use {{ to "escape" the curly brace. Writeln interprets {{0} as 'literal {' followed by 0}, resulting in the wrong format error.

like image 29
Sergey Kalinichenko Avatar answered Oct 13 '22 02:10

Sergey Kalinichenko