我怎样才能格式化字符串转换成.NET中固定宽度的字段? [英] How can I format a string into a fixed width field in .Net?

查看:240
本文介绍了我怎样才能格式化字符串转换成.NET中固定宽度的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想任意长度的字符串格式化为固定宽度字段的显示。

让我们用20的宽度,例如,并调用字符串是格式化的第我添加了格式化字符串到StringBuilder的命名湾

 昏暗b以新System.Text.StringBuilder()
昏暗参考译文]新建字符串
 

如果我想要显示的字符串是超过20个字符短,我可以做到这一点:

 在b.append(s.PadRight(20))
 

  b.AppendFormat({0,-20},S)
 

到目前为止,一切都很好。但是,如果字符串的长度超过20个字符,我希望字符串被截断为20个字符,因为它被追加。在code以上追加整个字符串。

我尝试这样做:

 在b.append(s.Substring(0,20).PadRight(20))
 

不过,这触发如果字符串是一个例外少于20个字符。

所以,我结束了:

 在b.append(s.PadRight(20).Substring(0,20))
 

这似乎做这项工作。该PadRight $ P $通过确保泰德字符串pvents除了有20个字符进行字串前面。

我在想,如果有,将看起来更优雅,并避免填充字符串只是让prevent从引起异常的子串的另一种方法。我错过了的String.Format的功能,可以在一个步骤做到这一点?

编辑补充解决方案:

我结束了以下code:

 模块扩展
    <扩展()> _
    功能AppendFixed(BYVAL b以StringBuilder的,BYVAL参考译文字符串,BYVAL宽度为整数)作为StringBuilder的
        如果s.Length> =宽度然后
            在b.append(S,0,宽度)
        其他
            在b.append(S)
            在b.append(,宽 -  s.Length)
        结束如果
        返回b
    端功能
前端模块
 

本使用扩展方法来清理语法,所建议的乔尔和Merlyn,并使用StringBulider追加重载,以避免产生新的字符串,将不得不被垃圾收集,所建议的supercat。感谢那些帮助。

解决方案
  

我在想,如果有那会显得更优雅的和避免填充替代方法的字符串

(强调)

 <扩展()> _
公共职能AppendFixed(BYVAL目标作为StringBuilder的,BYVAL值作为字符串,BYVAL desiredLength为整数)作为StringBuilder的
    如果value.Length< desiredLength然后value.PadRight(desiredLength)
    返回target.Append(value.Substring(0,desiredLength))
端功能
 

要使用它:

  b.AppendFixed(S,20)
 

I am trying to format a string of arbitrary length into a fixed width field for display.

Let's use a width of 20 as an example, and call the string to be formatted s. I'm adding the formatted string to a StringBuilder named b.

Dim b As New System.Text.StringBuilder()
Dim s as New String

If the string I want to display is shorter than 20 characters, I can do this:

b.Append(s.PadRight(20))

or

b.AppendFormat("{0,-20}", s)

So far, so good. But, if the string is longer than 20 characters, I want the string to be truncated to 20 characters as it is appended. The code above appends the entire string.

I tried this:

b.Append(s.Substring(0,20).PadRight(20))

But, this fires an exception if the string was shorter than 20 characters.

So, I ended up with:

b.Append(s.PadRight(20).Substring(0,20))

This seems to do the job. The PadRight prevents the exception by making sure thet string has 20 characters before the Substring is performed.

I was wondering if there is an alternate method that would look more elegant and avoid padding the string just so prevent the substring from causing an exception. Have I missed a feature of String.Format that can accomplish this in one step?

Edited to add solution:

I ended up with the following code:

Module Extensions
    <Extension()> _
    Function AppendFixed(ByVal b As StringBuilder, ByVal s As String, ByVal width As Integer) As StringBuilder
        If s.Length >= width Then
            b.Append(s, 0, width)
        Else
            b.Append(s)
            b.Append(" ", width - s.Length)
        End If
        Return b
    End Function
End Module

This uses an extension method to clean up the syntax, as suggested by Joel and Merlyn, and uses the StringBulider Append overloads to avoid creating new strings that will have to be garbage collected, as suggested by supercat. Thanks to those that helped.

解决方案

I was wondering if there is an alternate method that would look more elegant and avoid padding the string

(emphasis added)

<Extension()> _
Public Function AppendFixed(ByVal target As StringBuilder, ByVal value As String, ByVal desiredLength As Integer) As StringBuilder
    If value.Length < desiredLength Then value.PadRight(desiredLength)
    Return target.Append(value.Substring(0,desiredLength))
End Function

To use it:

b.AppendFixed(s, 20)

这篇关于我怎样才能格式化字符串转换成.NET中固定宽度的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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