gpt4 book ai didi

vb.net - 如何添加到 StringBuilder?

转载 作者:行者123 更新时间:2023-12-04 11:50:43 25 4
gpt4 key购买 nike

VB.NET 有一个方法,就像 java 一样,可以附加到类型为 的对象上。字符串生成器
但是我可以在这个对象前面加上一些字符串吗(我的意思是在 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)

最佳答案

您可以使用 StringBuilder.Insert 前置到字符串生成器:

Dim sb = New StringBuilder()
sb.Append("World")
sb.Insert(0, "Hello, ")
Console.WriteLine(sb.ToString())

这输出:
Hello, World

编辑

糟糕,注意到@dbasnett 在评论中说了同样的话......

关于vb.net - 如何添加到 StringBuilder?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25192686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com