现在的位置: 首页 > 综合 > 正文

C# String 前面不足位数补零的方法

2014年09月05日 ⁄ 综合 ⁄ 共 393字 ⁄ 字号 评论关闭

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

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

例如:

1、string aa = "wu";

string bb = aa.PadLeft(6);//右对齐此实例中的字符,在左边用空格填充以达到指定的总长度.
string cc = aa.PadLeft(6, '0');//右对齐此实例中的字符,在左边用指定的 Unicode 字符填充以达到指定的总长度。

2、Pznm =(snode.InnerText).ToString().PadLeft(10,'0');
字符串固定为10位,不足10位左边补零。

抱歉!评论已关闭.