如何使用C#在ASP.NET的特定长度字段的字符串生成器中为动态字符串添加空格? [英] How to add spaces to a dynamic string in a string builder for a specific length field in ASP.NET with C#?

查看:102
本文介绍了如何使用C#在ASP.NET的特定长度字段的字符串生成器中为动态字符串添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串生成器中为asp.net中的特定长度字段添加空格到c#



这里字符串是动态的,它将长度小于指定的长度,我需要添加指定长度的空格。



我尝试了什么:



 sbOAKAccp.Append(  01);  //  记录类型 
sbOAKAccp.Append ( 03); // 标准级别
sbOAKAccp.Append(dsXml.Tables [ EHR_M01]。行[ 0 ] [ TestFileIndicator]。ToString()); // 测试字段指示符
sbOAKAccp.Append(dsXml.Tables [ EHR_M01]。行[ 0 ] [ DestinationRoutingNumber]。ToString()); // 目的地路由号码
sbOAKAccp.Append(strFileName); // OCE / RCE文件名
sbOAKAccp.Append(dsXml.Tables [ EHR_M01]。行[ 0 ] [ FileCreationDate]。ToString()) ; // 文件创建日期
sbOAKAccp.Append(dsXml.Tables [ EHR_M01]。行[ 0 ] [ FileCreationTime]。ToString( )); // 文件创建时间
sbOAKAccp.Append( N); // 重新发送指标
sbOAKAccp.Append( 71 ); // ECE类型
sbOAKAccp.Append( 01 ); // ECE状态代码(拒绝-99)
sbOAKAccp .Append( 接受异常 .PadRight( 29 ' ')); // 集合状态描述
sbOAKAccp.Append(Environment.NewLine);

解决方案



控制间距

您可以使用以下语法定义插入到结果字符串中的字符串的宽度 {0,12} ,插入一个12个字符的字符串。在这种情况下,第一个对象的字符串表示在12个字符的字段中右对齐。 (但是,如果第一个对象的字符串表示长度超过12个字符,则忽略首选字段宽度,并将整个字符串插入到结果字符串中。)



控制对齐

默认情况下,如果指定字段宽度,字符串在其字段中右对齐。要在字段中左对齐字符串,请在字段宽度前加上负号,例如 {0,-12} ,以定义12个字符的右对齐字段。



例如,要将目的地路由号码填充到10个字符,末尾有空格:

 sbOAKAccp.AppendFormat({0,-10},dsXml.Tables [EHR_M01]。行[0] [DestinationRoutingNumber]); 



将文件名填充为30个字符,开头的空格为:

 sbOAKAccp.Append({0,30},strFileName); 


I want to add spaces to a dynamic string in a string builder for a specific length field in asp.net with c#

Here the string is dynamic and it will be of some length less than the specified length and I need to add spaces up to that specified length.

What I have tried:

sbOAKAccp.Append("01");//Record Type
sbOAKAccp.Append("03");//Standard Level
sbOAKAccp.Append(dsXml.Tables["EHR_M01"].Rows[0]["TestFileIndicator"].ToString());//Test Field Indicator
sbOAKAccp.Append(dsXml.Tables["EHR_M01"].Rows[0]["DestinationRoutingNumber"].ToString());//Destination Routing Number
sbOAKAccp.Append(strFileName);//OCE/RCE Filename
sbOAKAccp.Append(dsXml.Tables["EHR_M01"].Rows[0]["FileCreationDate"].ToString());//File Creation Date
sbOAKAccp.Append(dsXml.Tables["EHR_M01"].Rows[0]["FileCreationTime"].ToString());//File Creation Time
sbOAKAccp.Append("N");//Resend Indicator
sbOAKAccp.Append(71);//ECE Type
sbOAKAccp.Append("01");//ECE Status Code(Reject-99)
sbOAKAccp.Append("Accepted with exceptions".PadRight(29, ' '));//Collection Status Description
sbOAKAccp.Append(Environment.NewLine);

解决方案


Controlling spacing
You can define the width of the string that is inserted into the result string by using syntax such as {0,12}, which inserts a 12-character string. In this case, the string representation of the first object is right-aligned in the 12-character field. (If the string representation of the first object is more than 12 characters in length, though, the preferred field width is ignored, and the entire string is inserted into the result string.)

Controlling alignment
By default, strings are right-aligned within their field if you specify a field width. To left-align strings in a field, you preface the field width with a negative sign, such as {0,-12} to define a 12-character right-aligned field.


For example, to pad the destination routing number to 10 characters, with the spaces at the end:

sbOAKAccp.AppendFormat("{0,-10}", dsXml.Tables["EHR_M01"].Rows[0]["DestinationRoutingNumber"]);


To pad the filename to 30 characters, with the spaces at the start:

sbOAKAccp.Append("{0,30}", strFileName);


这篇关于如何使用C#在ASP.NET的特定长度字段的字符串生成器中为动态字符串添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆