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. Javascript 事件对象(二)event事件

    Event事件: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" ...

  2. WPF的Binding学习笔记(一)

    原文: http://www.cnblogs.com/pasoraku/archive/2012/10/20/2732427.html 一.binding的一般步骤 1,准备数据源     数据源需要 ...

  3. 诺基亚XL中Intent.ACTION_VIEW无效的问题

    今天测试播放视频的时候,发现在诺基亚XL机型里不能弹出视频应用列表. 我的代码是: Intent intent = new Intent(Intent.ACTION_VIEW); intent.set ...

  4. JS实现简单的图片切换效果

    使用图片进行点击切换效果 <!doctype html> <html lang="en"> <head> <meta charset=&q ...

  5. MVC的用法和作用

    最近在学习IOS项目的时候,老师经常提起MVC,在理解的过程中,越来越发现MVC的魅力,MVC:M:Model V:View C:Controller:Model 是用来存储数据的,View 是用来显 ...

  6. Jenkins Job 自杀 groovy

    下面的groovy可以加在post groovy script里面在job跑完的时候自杀(把本Job删掉) suicide_url="http://[USER]:[PASSWORD]@[JE ...

  7. MongoDB学习笔记三:查询

    MongoDB中使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.find的第一个参数决定了要返回哪些文档,其形式也是一个文档,说明要执行的查询细节.空的查询 ...

  8. 自动选中div中的文字

    <html> <head> <title></title> <script type="text/javascript" de ...

  9. python之类的属性

    class type(object) With one argument, return the type of an object. The return value is a type objec ...

  10. POJ-2726-Holiday Hotel

    Holiday Hotel   Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8302   Accepted: 3249 D ...