【发布时间】:2014-08-07 21:37:54
【问题描述】:
VB.NET 有一个方法,就像 java 一样,可以附加到 StringBuilder 类型的对象上 但是我可以在这个对象之前添加一些字符串吗(我的意思是在 stringbuilder 值之前添加一些字符串,而不是之后)。这是我的代码:
'Declare an array
Dim IntegerList() = {24, 12, 34, 42}
Dim ArrayBefore As New StringBuilder()
Dim ArrayAfterRedim As New StringBuilder()
ArrayBefore.Append("{")
For i As Integer = IntegerList.GetLowerBound(0) To IntegerList.GetUpperBound(0)
ArrayBefore.Append(IntegerList(i) & ", ")
Next
' Close the string
ArrayBefore.Append("}")
'Redimension the array (increasing the size by one to five elements)
'ReDim IntegerList(4)
'Redimension the array and preserve its contents
ReDim Preserve IntegerList(4)
' print the new redimesioned array
ArrayAfterRedim.Append("{")
For i As Integer = IntegerList.GetLowerBound(0) To IntegerList.GetUpperBound(0)
ArrayAfterRedim.Append(IntegerList(i) & ", ")
Next
' Close the string
ArrayAfterRedim.Append("}")
' Display the two arrays
lstRandomList.Items.Add("The array before: ")
lstRandomList.Items.Add(ArrayBefore)
lstRandomList.Items.Add("The array after: ")
lstRandomList.Items.Add(ArrayAfterRedim)
如果您查看我的代码的最后 4 行,我想在我的列表框控件中的一行中添加字符串生成器之前的文本。所以代替这个:
lstRandomList.Items.Add("The array before: ")
lstRandomList.Items.Add(ArrayBefore)
我想要这样的东西:
lstRandomList.Items.Add("The array before: " & ArrayBefore)
【问题讨论】:
-
不是很清楚问题是什么。试试
"The array before: " & ArrayBefore.ToString。 -
Stringbuilder 有一个 Insert 方法,所以如果你在 0 处插入,你就是在等待。
-
为什么要重新调整阵列?你真的想在这里做什么?
标签: vb.net string stringbuilder prepend