static void Main()
{
string c=Console.ReadLine();
string d=Console.ReadLine();
Console.WriteLine(c+","+d); //用“+”连接符
}

  

你说这样写很容易写错,很麻烦,C#还提供另一种书写方式,就是占位符,用{ }来表示,在{ }内填写所占的位的序号,C#规定从0开始,也就是说刚才那中输出,我们还可以这样来表示
Response.Write(“{0},{1}”,c,d); 
在这里有两个位c,d,那么也就需要两个占位符所以我们写成{0},{1},还需要注意的是,占位符要写在””内。
static void Main()
{
string c=Console.ReadLine();
string d=Console.ReadLine();
string m=String.Format(“{0},{1}”,c,d); //字符串格式输出
Response.Write(m);
}

  

可以看出输出结果是完全一样的。在这里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   16进制格式
protected void Page_Load(object sender, EventArgs e)
{
int i=12345;
Response.Write("{0:C}",i); //货币
Response.Write("{0:D}",i); //十进制数
Response.Write("{0:E}",i); //科学技术法
Response.Write("{0:F}",i); // 浮点数表示法
Response.Write("{0:G}",i); //G或g General 常用格式
Response.Write("{0:N}",i); //N或n 用逗号分割千位的数字
}

  

1.数值的处理与格式化
   当检查用户输入的数据类型是哪一种数据时,只需要调用值类型的TryParse()方法来检查数据内容。
   TryParse()和Parse()方法的区别是,前者会返回一个bool值,后者如转换不成功,则抛出异常,需要用try catch语句捕捉。
   下面是检查用户输入内容是否为数字

    protected void Page_Load(object sender, EventArgs e)
{
string s1 = "三百";
string s2 = "1000";
string s3 = "2,000";
string s4 = "-1000";
string s5 = "4000d";
int currentValue = 0;
Response.Write(int.TryParse(s1, out currentValue)); //返回False
Response.Write(int.TryParse(s2, out currentValue)); //返回True
Response.Write(int.TryParse(s3, out currentValue)); //返回False
Response.Write(int.TryParse(s4, out currentValue)); //返回True
Response.Write(int.TryParse(s5, out currentValue)); //返回False
}

  

数值的格式化:
    若有decimal和float类型数处理,则要使用后置字符M或F,如:
    decimal d = 12345.67M;
    float f =3.1415F;
    否则,会把数字视为 double 类型处理,产生编译错误,若是整数加不加M都可以。
2.字母的处理与格式化
protected void Page_Load(object sender, EventArgs e)
{
string str = "Do you like ASP.NET?";
Response.Write(str.ToUpper()); //转换为大写
Response.Write(str.ToLower()); //转换为小写
}

  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(年月模式)

static void Main()
{
Response.Write("{0:D}",DateTime.Now); //输出到天
Response.Write("{0:y}",DateTime.Now); //输出到月
Response.Write("{0:m}",DateTime.Now); //取出是那个月
Response.Write("{0:T}",DateTime.Now); // 取长时间到秒
Response.Write("{0:t}",DateTime.Now); //取短时间到分
Response.Write("{0:tt}",DateTime.Now); //取出是上午还是下午
}

  

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

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

    原文地址:http://www.cnblogs.com/fumj/articles/2380290.html 在c#中有两种方式可以输出多个字符 其中的一种: static void Main()   ...

  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. 12-4mysql 查询

    简单查询select * from 表名; 注意:*代表所有); 查询指定列 select 列名,列名 from 表名 修改结果集的列名select 列名 as'',列名 as'' from 表名 条 ...

  2. python数据结构与算法——栈

    # 栈# 其实python里面的list就可以当栈使用啦,用collections.deque也可以# 1. 入栈 list.append(item)# 2. 出栈 item = list.pop() ...

  3. 史上最详细Windows版本搭建安装React Native环境配置 转载,比官网的靠谱亲测可用

    史上最详细Windows版本搭建安装React Native环境配置   2016/01/29 |  React Native技术文章 |  Sky丶清|  95条评论 |  33530 views ...

  4. 2009年到2013年甲子园ED

    2009年 2010年  最喜欢的一个!看过N遍 2011年 也不错! 2012年  超级好听啊~^_^比10年的还好,看过N+1遍……o(╯□╰)o 2013年春季甲子园 2013年夏季 印象最深的 ...

  5. pods的安装和使用

    ////  pods的安装.h//  IOS笔记 /*Cocoapods安装步骤 1.升级Ruby环境 终端输入:$gem update --system 此时会出现 ERROR:  While ex ...

  6. checkbox提交多组数据到action

    突然想通过checkbox来提交多组数据到action,一时间想不起来怎么写,到网上流岚大婶们的笔迹之后,有了新发现! 方法一: 在action用一个String类型的变量来接受checkbox传过来 ...

  7. javascript 中的 true 或 false

    JavaScript中奇葩的假值 通常在以下语句结构中需要判断真假 if分支语句 while循环语句 for里的第二个语句 如 1 2 3 4 5 6 7 if (boo) { // do somet ...

  8. [转]UDP穿透NAT的原理与实现(UDP“打洞”原理)

    NAT(The IP Network Address Translator) 的概念和意义是什么? NAT, 中文翻译为网络地址转换.具体的详细信息可以访问RFC 1631 - http://www. ...

  9. bootstrap表格内容垂直居中

    td{ vertical-align: middle !important;}

  10. jQuery中的事件机制深入浅出

    昨天呢,我们大家一起分享了jQuery中的样式选择器,那么今天我们就来看一下jQuery中的事件机制,其实,jQuery中的事件机制与JavaScript中的事件机制区别是不大的,只是,JavaScr ...