龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

ASP.NET中PadLeft、PadRight补足位数函数的用法

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
有时候,为了让格式统一,当位数不足时,给予补足。比如:2008-01-01 13:42:05,这其中就对月、日、秒进行了补位。 在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。 PadLeft

有时候,为了让格式统一,当位数不足时,给予补足。比如:2008-01-01 13:42:05,这其中就对月、日、秒进行了补位。

在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。

PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度

PadLeft(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足 totalWidth 长度

示例:

h = h.PadLeft(2, '0');

注意第二个参数为 char 类型,所以用单引号,也可以用 Convert.ToChar(string value) 把字符串转换成 char 类型。如果字符串长度大于 1,则使用 str.ToCharArray()[index]。

精彩图集

赞助商链接