您现在的位置是:首页
>
字符串的格式化方法 ASP.NET 中字符的格式化输出
ASP.NET 中字符的格式化输出 可以使用 Format 方法将字符串表示为规定格式 规定格式的一般形式为 {N [ M][: formatCode]}其中 N 是从零开始的整数 表示第几个参数 M

ASP.NET 中字符的格式化输出
可以使用 Format 方法将字符串表示为规定格式 规定格式的一般形式为 {N [ M][: formatCode]}其中 N 是从零开始的整数 表示第几个参数 M 是一个可选整数 表示最小宽度 若该参数的长度小于M 就用空格填充 如果 M 为负 则左对齐 如果 M 为正 则右对齐 如果未指定 M 则默认为零 formatCode 是可选的格式化代码字符串 (详细内容请搜索 格式化字符串 查看)必须用 { 和 } 将格式与其他字符分开 如果恰好在格式中也要使用大括号 可以用连续的两个大括号表示一个大括号 即 {{ 或者 }} 常用格式举例 ( ) int i= ; this textBox Text=i ToString();//结果 (this指当前对象 或叫当前类的实例)this textBox Text=i ToString( d ); //结果 ( ) int i= ;double j= ;string s =string Format( the value is { :d} i);string s =string Format( the value is { :f } j);this textBox Text=s ;//结果 the value is this textBox Text=s ;//结果 the value is ( )double i= ;this textBox Text=i ToString( f ); //结果 this textBox Text=i ToString( f ); //结果 ( )double i= ;this textBox Text=i ToString( n ); //结果 this textBox Text=i ToString( n ); //结果 ( )double i= ;string s=string Format( the value is { :p} i);this textBox Text=i ToString( p ); //结果 %this textBox Text=s; //结果 the value is % ( ) DateTime dt =new DateTime( ); this textBox Text=dt ToString( yy M d );//结果 this textBox Text=dt ToString( yyyy年M月 );//结果 年 月( ) int i= ;double j= ;string s=string Format( i:{ } j:{ } i j); // 表示左对齐 占 位this textBox Text=s ; //结果i: j: lishixinzhi/Article/program/net/201311/13323 很赞哦! (1048)