惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threat Research - Cisco Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
博客园 - Franky
V2EX - 技术
V2EX - 技术
V
V2EX
T
Tailwind CSS Blog
P
Privacy International News Feed
S
Securelist
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
C
CXSECURITY Database RSS Feed - CXSecurity.com
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
月光博客
月光博客
T
The Blog of Author Tim Ferriss
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Simon Willison's Weblog
Simon Willison's Weblog
C
Check Point Blog
Cisco Talos Blog
Cisco Talos Blog
Recorded Future
Recorded Future
L
LangChain Blog
The Hacker News
The Hacker News
Project Zero
Project Zero
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
Latest news
Latest news
S
Schneier on Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 叶小钗
C
Cisco Blogs
I
Intezer
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Register - Security
The Register - Security
I
InfoQ
Engineering at Meta
Engineering at Meta
博客园 - 司徒正美
小众软件
小众软件
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
TaoSecurity Blog
TaoSecurity Blog

博客园 - 牵牛望岳

ICloneable 接口--c# 深复制与浅复制 IDisposable接口详解 listbox控件的一些操作 IsPostBack用法,Page.IsValid的用法 IIf函数 替换、恢复Html中的特殊字符 C#连接数据库的一些鲜为人知的方法 C#关键字 【推荐】常用 SQL 语句大全 CSS中的黄金分割率 几条相关SQL语句 解析.net中ref和out的实质 JavaScript substr() 和 substring() 方法的区别 武林外传搞笑语录 调试和跟踪 VS2005单元测试[转] 几个.Net开源的CMS、Portal系统 DataKeyNames工作 查询后,翻页问题的解决办法2。(GridView1.PageIndex = e.NewPageIndex;) (转)
C# 中用 PadLeft、PadRight 补足位数
牵牛望岳 · 2009-04-16 · via 博客园 - 牵牛望岳

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

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

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

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

示例:

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

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