在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#占位符与格式化字符串

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

  2. Android 图片加载库Glide 实战(二),占位符,缓存,转换自签名高级实战

    http://blog.csdn.net/sk719887916/article/details/40073747 请尊重原创 : skay <Android 图片加载库Glide 实战(一), ...

  3. Spring拓展接口之BeanFactoryPostProcessor,占位符与敏感信息解密原理

    前言 开心一刻 一只被二哈带偏了的柴犬,我只想弄死隔壁的二哈 what:是什么 BeanFactoryPostProcessor接口很简单,只包含一个方法 /** * 通过BeanFactoryPos ...

  4. PHP中函数sprintf .vsprintf (占位符)

    sprintf()格式化字符串写入一个变量中. vsprintf()格式化字符串些写入变量中. <?php $num1 = 123; $num2 = 456; $txt = vsprintf(& ...

  5. spring占位符解析器---PropertyPlaceholderHelper

    一.PropertyPlaceholderHelper 职责 扮演者占位符解析器的角色,专门用来负责解析路劲中or名字中的占位符的字符,并替换上具体的值 二.例子 public class Prope ...

  6. Hibernate占位符警告:use named parameters or JPA-style positional parameters instead.

    Hibernate占位符警告:use named parameters or JPA-style positional parameters instead. >>>>> ...

  7. Android之使用JAVA占位符格式数据(很实用)

    小编虽然是学java出生,但工作之后就一直从事android开发,很多java基础都忘记完了,最近一年从ES换到了AS,原来的很多习惯都收到了挑战,比如我喜欢ES写方法的时候先在JAVA projec ...

  8. 标准库bind函数中使用占位符placeholders

    placeholders ,占位符.表示新的函数对象中参数的位置.当调用新的函数对象时,新函数对象会调用被调用函数,并且其参数会传递到被调用函数参数列表中持有与新函数对象中位置对应的占位符. 举个例子 ...

  9. 深入Spring Boot:那些注入不了的Spring占位符(${}表达式)

    Spring里的占位符 spring里的占位符通常表现的形式是: <bean id="dataSource" destroy-method="close" ...

随机推荐

  1. Codeforces 148D Bag of mice 概率dp(水

    题目链接:http://codeforces.com/problemset/problem/148/D 题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢 ...

  2. how to translate the text of push button

    Background:In a project, the need to translate the buttons on the screen, as shown below,the followi ...

  3. oracle修改闪回日志的位置

    改变闪回日志位置的步骤: A.Change the value of the DB_RECOVERY_FILE_DEST initialization parameter to a new value ...

  4. js获取网页高度和宽度(备份)

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  5. uva 10655 - Contemplation! Algebra

    ---恢复内容开始--- Given the value of a+b and ab you will have to find the value of an+bn 给出a+b和a*b的值,再给出n ...

  6. for练习--侦察兵

    static void Main21侦察兵(string[] args) { //某侦察队接到一项紧急任务,要求在A.B.C.D.E.F六个队员中尽可能多地挑若干人,但有以下限制条件: //侦察兵A和 ...

  7. 利用java生成带有干扰线的网页验证码图片

    package imageCreate; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import j ...

  8. 关于ECharts Java类库的一个jquery插件

    在项目中开发图表功能时用到了Echars和一个关于Echars的java类库(http://git.oschina.net/free/ECharts).这个类库主要目的是方便在Java中构造EChar ...

  9. boost信号量 boost::interprocess::interprocess_semaphore的用法

    使用方法首先给信号量初始化赋值,可以根据需要设定需要的值,之前在写项目的过程中用这个控制下载的线程个数. boost::interprocess::interprocess_semaphore m_s ...

  10. C语言中的数据类型

    基本数据类型: int float double char void 派生数据类型: 数据类型修饰符 + 基本数据类型 = 派生数据类型 signed  和 unsigned 类型 unsigned ...