programing

C #의 문자열에서 {0}은 (는) 무엇을 의미합니까?

nasanasas 2020. 10. 4. 11:28
반응형

C #의 문자열에서 {0}은 (는) 무엇을 의미합니까?


다음과 같은 사전에서 :

Dictionary<string, string> openWith = new Dictionary<string, string>();

openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

Console.WriteLine("For key = \"rtf\", value = {0}.", openWith["rtf"]);

출력은 다음과 같습니다.

키 = "rtf"값 = wordpad.exe

무슨 {0}뜻입니까?


형식화 된 문자열을 인쇄하고 있습니다. {0}는 형식 문자열 다음에 첫 번째 매개 변수를 삽입하는 것을 의미합니다. 이 경우 "rtf"키와 연관된 값입니다.

비슷한 String.Format의 경우

//            Format string                    {0}           {1}
String.Format("This {0}.  The value is {1}.",  "is a test",  42 ) 

"This is a test . The value is 42 " 문자열을 생성합니다 .

표현식을 사용하고 값을 여러 번 인쇄 할 수도 있습니다.

//            Format string              {0} {1}  {2}
String.Format("Fib: {0}, {0}, {1}, {2}", 1,  1+1, 1+2) 

"Fib : 1 , 1 , 2 , 3 " 산출

복합 형식에 대해 설명하는 http://msdn.microsoft.com/en-us/library/txafckwd.aspx 에서 자세한 내용을 참조하십시오 .


문자열의 자리 표시 자입니다.

예를 들면

string b = "world.";

Console.WriteLine("Hello {0}", b);

이 출력을 생성합니다.

Hello world.

또한 원하는만큼 많은 자리 표시자를 가질 수 있습니다. 이것은 또한 작동합니다 String.Format:

string b = "world.";
string a = String.Format("Hello {0}", b);

Console.WriteLine(a);

그리고 여전히 동일한 결과를 얻을 수 있습니다.


인쇄하려는 값 {0} {1},, 등 외에 형식을 지정할 수 있습니다. 예를 들어 {0,4}는 4 개의 공백으로 채워진 값입니다.

여러 가지 기본 제공 형식 지정자가 있으며 추가로 직접 만들 수 있습니다. 괜찮은 자습서 / 목록은 C #의 문자열 서식을 참조하세요 . 또한 여기 에 FAQ가 있습니다 .


나중에 참조 할 수 있도록 Visual Studio에서 메서드 이름 (예 : WriteLine)에 커서를 놓고 키를 눌러 F1해당 컨텍스트에 대한 도움말을 표시 할 수 있습니다. String.Format()이 경우 주변을 파헤쳐 보면 많은 유용한 정보와 함께 당신을 찾을 수 있습니다 .

선택 항목을 강조 표시 (예 : 더블 클릭 또는 드래그 선택 수행)하고 치면 F1컨텍스트가 아닌 문자열 검색 (유용한 것을 찾는 데 짜증나는 경향이 있음) 만 수행하므로 커서를 내부 아무 곳에 나 배치해야합니다. 강조 표시하지 않고 단어.

이것은 클래스 및 기타 유형에 대한 문서화에도 유용합니다.


첫 번째 매개 변수의 자리 표시 자이며 귀하의 경우에는 "wordpad.exe"로 평가됩니다.

추가 매개 변수가있는 경우 {1}, 등을 사용합니다 .


It's a placeholder for a parameter much like the %s format specifier acts within printf.

You can start adding extra things in there to determine the format too, though that makes more sense with a numeric variable (examples here).


This is what we called Composite Formatting of the .NET Framework to convert the value of an object to its text representation and embed that representation in a string. The resulting string is written to the output stream.

The overloaded Console.WriteLine Method (String, Object)Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream using the specified format information.

참고URL : https://stackoverflow.com/questions/530539/what-does-0-mean-when-found-in-a-string-in-c

반응형