日期:2014-05-20  浏览次数:20959 次

格式化字符串的问题?
printf( "%8dm ",2);   输出:   -------2m,-代表空格。
printf( "%-8dm ",2);输出:   2-------m,-代表空格。
我想用C#实现与上面一样的效果:
String.Format( "{0:D8}m ",2);   输出00000002m。
是用0来添充空位,而且也无法使数据左对齐。
怎样才能用空格来添充空位和左对齐呢?


------解决方案--------------------
String.Format( "{0,-8} ", 2)
------解决方案--------------------
There really isn 't any formatting within a strong, beyond it 's alignment. Alignment works for any argument being printed in a String.Format call.



Sample Generates
String.Format( "-> {1,10} <- ", "Hello "); -> Hello <-
String.Format( "-> {1,-10} <- ", "Hello "); -> Hello <-



------解决方案--------------------
try..

string str = "2m ";
str = str.PadLeft(10, ' ');