C# PadLeft、PadRight的使用

C# 中用 PadLeft、PadRight 为了让格式统一,当位数不足时,给予补足

返回一个新字符串,该字符串通过在此实例中的字符左侧填充空格来达到指定的总长度,从而实现右对齐:

public string PadLeft(int totalWidth);

PadLeft(int totalWidth) 在字符串左边用 空格 补足 totalWidth 长度

"1".PadLeft(6);    //输出     1,字符串为右对齐,用空格填充

"123456".PadLeft(6); //输出123456,字符串为右对齐,用空格填充

返回一个新字符串,该字符串通过在此实例中的字符左侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符右对齐:PadLeft(int totalWidth, char paddingChar) 在字符串左边用 paddingChar 补足 totalWidth 长度

public string PadLeft(int totalWidth, char paddingChar);

"1".PadLeft(6, '0');      //输出 000001,字符串为右对齐,长度不足用0填充

"12345678".PadLeft(6, '0');  //输出 12345678,totalWidth小于原字符串长度,直接返回原字符串。

"★".PadLeft(6, '☆');     //输出 ☆☆☆☆☆★

返回一个新字符串,该字符串通过在此字符串中的字符右侧填充空格来达到指定的总长度,从而使这些字符左对齐:PadRight(int totalWidth); 在字符串右边用 空格 补足 totalWidth 长度

public string PadRight(int totalWidth);

"1".PadRight(6);    //输出1     ,字符串为左对齐,用空格填充

"123456".PadRight(6); //输出123456,字符串为左对齐,用空格填充

返回一个新字符串,该字符串通过在此字符串中的字符右侧填充指定的 Unicode 字符来达到指定的总长度,从而使这些字符左对齐:PadRight(int totalWidth, char paddingChar) 在字符串右边用 paddingChar 补足 totalWidth 长度

"1".PadRight(6, '0');     //输出 100000,字符串为左对齐,长度不足用0填充

"12345678".PadRight(6, '0'); //输出 12345678,totalWidth小于原字符串长度,直接返回原字符串。

"★".PadRight(6, '☆');    //输出 ★☆☆☆☆☆

Guess you like

Origin blog.csdn.net/BYH371256/article/details/120410516