首页 > 代码库 > [CSharp]1 占位符、转义字符
[CSharp]1 占位符、转义字符
C# Placeholder
There are 2 ways to output multiple characters in C#.
One:
static void Main()
{
string c=Console.ReadLine();
string d=Console.ReadLine();
Console.WriteLine(c+","+d); //Use "+" as a connector
}
Two:
C# also provides another notation that is a placeholder.Use {} to represent,and fill in the number of bits occupied in {}.C# provisions starting from 0.
Console.WriteLine("{0},{1}",c,d);
In addition to using WriteLine() to output,you can use the output string format.
static void Main()
{
string c=Console.ReadLine();
string d=Console.ReadLine();
string m=String.Format("{0}",c);
string n=String.Format("{0}",d);
Console.WriteLine(m+","+n);
}
The following summarizes several common format identifier.
C or c Currency Format
D or d Decimal Format
E or e Exponent Format
F or f Fixed Point Format
G or g General Format
N or n Comma-separated One Thousand Numbers
P or p Percentage
R or r Round-trip
X or x Hex
Example
static void Main()
{
int i=12345;
Console.WriteLine("{0:C}",i);
Console.WriteLine("{0:D}",i);
Console.WriteLine("{0:E}",i);
Console.WriteLine("{0:F}",i);
Console.WriteLine("{0:G}",i);
Console.WriteLine("{0:N}",i);
}
d MM/dd/yyyy Short date pattern
D dddd,MMMM dd,yyyy Long date pattern
F dddd,MMMM dd,yyyy HH:mm Full date and time(long date and short time)
F dddd,MMMM dd,yyyy HH:mm:ss Full date time pattern(long date and long time)
G MM/dd/yyyy HH:mm General(short date and short time)
G MM/dd/yyyy HH:mm:ss General(short date and long time)
M,m MMMM dd Month day pattern
R,r ddd,dd MMM yyyy,HH‘:‘mm‘:‘ss ‘GMT‘ RFC1123Pattern
S yyyy-MM-dd HH:mm:ss SortableDateTimePattern(conforms to ISO 8601) using local time
T HH:mm Short time pattern
T HH:mm:ss Long time pattern
U yyyy-MM-dd HH:mm:ss Universal Sortable-date time pattern
U dddd,MMMM dd,yyyy,HH:mm:ss Universal Sortable-date time pattern
Y,y MMMM,yyyy Year month pattern
static void Main()
{
Console.WriteLine("{0:D}",DateTime.Now);
Console.WriteLine("{0:y}",DateTime.Now);
Console.WriteLine("{0:m}",DateTime.Now);
Console.WriteLine("{0:T}",DateTime.Now);
Console.WriteLine("{0:t}",DateTime.Now);
Console.WriteLine("{0:tt}",DateTime.Now);
}
The escape charcter
A special charcter constants
Backslash "\" at the beginning,followed by one or several characters.
Have a particular meaning,the meaning is different from the character of the original,so called"escape" character.
The main control codes used to represent those characters are not easy to use general representation.
Its roles is followed by the elimination of the original meaning of the character.
\’ Single quotes
\” Double quotes
\\ Backslash
\0 Null
\a Warning
\b Backspace
\f Feed
\n Newline
\r Enter
\t Horizontal tab
\v Vertical tab
In C #, "c: \ \ temp"means that the pathisc: \ temp;while@ "c: \ temp"saysc: \ temp;
@‘s Role isto ignorethe escape character.
[CSharp]1 占位符、转义字符