在c sharp中使用string.Format [英] Use of string.Format in c sharp

查看:255
本文介绍了在c sharp中使用string.Format的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释以下代码。这里dra是临时表的一行。我可以使用convert,cast或parse方法而不是string.Format。如果可能的话,请举例说明。





Please explain the following code. Here dra is row of a temp table. Can I use convert, cast or parse method instead of string.Format. If possible please explain with example.


DateTime datt = Convert.ToDateTime(DTLocalSc.Rows[0][4]);
dra["dat"] = string.Format("{0:dd-MM-yyyy}", datt);

推荐答案

要解释的是什么?

从控件或数组中获取字符串日期,将其转换为DateTime值,然后将其格式化为显示为日 - 月 - 年。您之前可能没有看过的唯一一点是在string.Format字符串中使用格式化表达式:

What''s to explain?
It gets a string date from a control, or array, converts it to a DateTime value, then formats it for the display as day-month-year. The only bit that you might not have seen before is the use of a formatting expression in a string.Format string:
{0:dd-MM-yyyy}

0数字将提供信息的参数

冒号指定格式字符串如下

dd-MM-yyyy部分指定实际用于DateTime值的格式。



这里有一个DateTime格式字符串的可能值列表:格式化日期时间以显示格式字符串描述 [ ^ ]

the 0 numbers the parameter that will supply the info
The colon specifies a format string is following
The dd-MM-yyyy part specifies the actual format to use for the DateTime value.

There is a list of the possible values for the DateTime formatting string here: Formatting a DateTime for display - format string description[^]


如果 dat 字段的类型是字符串(例如 varchar ),那么你必须使用的String.Format 。我强烈建议你阅读文档 [ ^ ]。
If the type of dat field is string (e.g. varchar) then you have to use String.Format. I strongly suggest you reading the documentation[^].


这篇关于在c sharp中使用string.Format的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆