0

很久没有用 VB.NET 写程序了,现在我想写一个 WinForm 应用程序。我有几个字符串,我想把它们放在一起,以便它们可以添加到ListView控件中。

我有这个:

text1 = ("00 | 34123 | 232")
text2 = ("023 | 233 | 23332 ")
text2 = ("00 | 2342432 | 122 ")

但我想要这个:

text1 = ("00     |     34123       |  232   ")
text2 = ("023    |     233         |  23332 ")
text2 = ("00     |     2342432     |  122   ")

请注意,每个数字都是一个字符串变量,因此每个数字都可以是:“12”或“123”或“1234”......我怎么能这样做!?

4

2 回答 2

1

您想使用String.PadRight方法。

示例用法:

Dim result = "00".PadRight(7) & "|    " _
             & "34123".PadRight(12) & "|  " _
             & "232".PadRight(6)

在这种情况下,这种String.Format方法甚至更好。请注意,要使这两种方法都起作用,您需要使用固定宽度的字体。

于 2012-12-03T20:21:10.783 回答
1
Public Function ToFixedColumns(ByVal input As String) As String
    'Separate individual items 
    Dim values = input.Split("|"c).Select(Function(s) s.Trim()).ToArray()

    'Validate split operation
    If values.Length <> 3 Then 
        Throw New InvalidArgumentException("The string was not in the correct starting format.")
    End If

    'Create new formatted string
    Return String.Format("{0,-6} |     {1,-12}  |  {2,-6}", _
           values(0),values(1),values(2))
End Function
于 2012-12-03T20:25:14.603 回答