原文地址:http://www.cnblogs.com/fumj/articles/2380290.html

在c#中有两种方式可以输出多个字符

其中的一种:

static void Main()  
         {
                   string c=Console.ReadLine();
                   string d=Console.ReadLine();
                  Console.WriteLine(c+","+d);    //用“+”连接符
         }
那么你说这样写很容易写错,很麻烦,C#还提供另一种书写方式,就是占位符,用{ }来表示,在{ }内填写所占的位的序号,C#规定从0开始,也就是说刚才那中输出,我们还可以这样来表示
Console.WriteLine(“{0},{1}”,c,d); //使用占位符的例子
在这里有两个位c,d,那么也就需要两个占位符所以我们写成{0},{1},还需要注意的是,占位符要写在””内。
除了使用WriteLine()来输出,当然我们还可以使用字符串格式输出,例如上面的程序完全可以写成(见cs_4.cs)
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);    //用“+”连接符
         }
可以看出输出结果是完全一样的。在这里String是一个类,Format是其中的一个方法用来格式化输出字符。
我们知道在现实的生活中有时候需要特殊的表示字符,例如表示货币,时间,那该怎么办呢?不用担心,C#中又格式化标识符,下面给大家介绍几个常用的格式化标识符
字母 含义
C或c Currency 货币格式
D或d Decimal 十进制格式(十进制整数,不要和.Net的Decimal数据类型混淆了)
E或e Exponent 指数格式
F或f Fixed point 固定精度格式
G或g General 常用格式
N或n 用逗号分割千位的数字,比如1234将会被变成1,234
P或p Percentage 百分符号格式
R或r Round-trip 圆整(只用于浮点数)保证一个数字被转化成字符串以后可以再被转回成同样的数字
X或x Hex 16进制格式
示例如下(见cs_6.cs)
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);   //G或g General 常用格式
                   Console.WriteLine("{0:N}",i);   //N或n 用逗号分割千位的数字
         }
d MM/dd/yyyy ShortDatePattern(短日期模式)
D dddd,MMMM dd,yyyy LongDatePattern(长日期模式)
F dddd,MMMM dd,yyyy HH:mm Full date and time (long date and short time)(全日期和时间模式)
F dddd,MMMM dd,yyyy HH:mm:ss FullDateTimePattern (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 MonthDayPattern(月天模式)
r,R ddd,dd MMM yyyy,HH':'mm':'ss 'GMT' RFC1123Pattern (RFC1123模式)
S yyyy-MM-dd HH:mm:ss SortableDateTimePattern (conforms to ISO 8601) using local time(使用本地时间的可排序模式)
T HH:mm ShortTimePattern (短时间模式)
T HH:mm:ss LongTimePattern(长时间模式)
U yyyy-MM-dd HH:mm:ss UniversalSortable-DateTimePattern (conforms to ISO 8601) using universal time(通用可排序模式)
U dddd,MMMM dd,yyyy,HH:mm:ss UniversalSortable-DateTimePattern(通用可排序模式)
y,Y MMMM,yyyy YearMonthPattern(年月模式)
示例如下:(见cs_5.cs)
         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);   //取出是上午还是下午     
         }

C#占位符与格式化字符串的更多相关文章

  1. C#占位符和格式化字符串

    static void Main() { string c=Console.ReadLine(); string d=Console.ReadLine(); Console.WriteLine(c+& ...

  2. python基础之二:占位符、格式化输出、while else 、逻辑运算

    1.占位符和格式化输出 示例代码 #格式化输出 # % s d # name = input('请输入姓名') # age = input('请输入年龄') # height = input('请输入 ...

  3. python基础学习之简化占位符和格式化的概念

    简化版占位符有哪些? %s   字符串占位符,代表该位置有一个字符串待替换(万能) %d   数字占位符,代表该位置有一个数字待替换 %f    同上,这里是浮点数,float[默认保留6位小数] % ...

  4. python语法_使用占位符进行格式化输出

    “%s”   占位符 name = input("name:") age = input("age:") job = input("job:" ...

  5. GDScript 格式化字符串

    GDScript offers a feature called format strings, which allows reusing text templates to succinctly c ...

  6. 第3.8节 Python百分号占位符的字符串格式化方法

    一.    概念         格式化字符串就是将一些变量转换为字符串并按一定格式输出字符串,包括指定字符的位置.对齐方式.空位补充方式等.Python提供了多种字符串格式设置方法.本节先介绍一种简 ...

  7. python 格式化输出详解(占位符:%、format、f表达式)——上篇 理论篇

    0 - 占位符介绍 要实现字符串的拼接,使用占位符是的一种高效.常用的方式. 举个例子,下面是不使用占位符的一种写法,直接使用加号拼接字符串 name = "Li hua" age ...

  8. Java使用占位符拼接字符串

    大家知道,在C#编程中,可以用占位符来拼接字符串,用起来非常的方便. 特别是需要进行大量的参数拼接的时候,比如: Console.WriteLine(String.Format("该域名{0 ...

  9. python基础入门--input标签、变量、数字类型、列表、字符串、字典、索引值、bool值、占位符格式输出

    # 在python3 中: # nian=input('>>:') #请输入什么类型的值,都成字符串类型# print(type(nian)) # a = 2**64# print(typ ...

随机推荐

  1. Javascript中函数及变量定义的提升

    <html> <head> <title>函数提升</title> <script language="javascript" ...

  2. nginx指定配制文件

    nginx启动: 未指定配制文件: ./nginx 指定配制文件: /usr/local/nginx/sbin/nginx -c /home/deploy/nginx-wz/conf/nginx.co ...

  3. [Ubuntu] Ubuntu13.04, the desktop freezed after login

    My os version is Ubuntu13.04, today, after started and logined, my desktop freezed. But i can still ...

  4. Tomcat增加缓存

  5. 【python cookbook】【数据结构与算法】9.在两个字典中寻找相同点

    问题:寻找两个字典中间相同的地方(相同的键.相同的值等) 解决方案:通过keys()或者items()方法来执行常见的集合操作(比如求并集.交集和差集)

  6. html5 canvas 笔记三(绘制文本和图片)

    绘制文本 fillText(text, x, y [, maxWidth])   在指定的(x,y)位置填充指定的文本,绘制的最大宽度是可选的. strokeText(text, x, y [, ma ...

  7. vs2010 仿XCode风格的头注释宏

    Sub DocumentFileHeader() Dim star As String star = "//***************************************** ...

  8. unicode转码,如:\u6d4b\u8bd5转成中文“测试”

    public static void main(String[] args) { String s = "测试"; String tt = gbEncoding(s); // St ...

  9. iOS抓包Charles 操作

    今天就来看一下Mac上如何进行抓包,之前有一篇文章介绍了使用Fidder进行抓包 http://blog.csdn.net/jiangwei0910410003/article/details/198 ...

  10. destoon 会员整合Ucenter/Discuz!/PHPWind教程

    首先进入 Destoon网站后台 -〉会员管理 -〉模块设置 -〉会员整合 假如需要整合的主站地址为 http://www.abc.com 论坛为 http://bbs.abc.com 1.整合Uce ...