vb.net 如何添加到 StringBuilder?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25192686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:00:23  来源:igfitidea点击:

How to prepend to a StringBuilder?

vb.netstringstringbuilderprepend

提问by Amaynut

VB.NET have a method, just like java, to append to an object of type StringBuilderBut can I prepend to this object (I mean a add some string before the stringbuilder value, not after). Here is my code:

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)

If you look at the last 4 lines of my code, I want to add the text just before the string builder all in one line in my list box control. So instead of this:

如果您查看我的代码的最后 4 行,我想在我的列表框控件中的一行中在字符串生成器之前添加文本。所以而不是这个:

 lstRandomList.Items.Add("The array before: ")
    lstRandomList.Items.Add(ArrayBefore)

I want to have something like this:

我想要这样的东西:

lstRandomList.Items.Add("The array before: " & ArrayBefore)

回答by Mark

You can use StringBuilder.Insertto prepend to the string builder:

您可以使用StringBuilder.Insert预先添加到字符串生成器:

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

This outputs:

这输出:

Hello, World

EDIT

编辑

Oops, noticed @dbasnett said the same in a comment...

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

回答by Luke

You can append your StringBuilder(after converting it to a string) to a new StringBuildercontaining the string you wish to prepend. The result is effectively the same as prepending, it just requires a new StringBuilder.

您可以将您的StringBuilder(将其转换为字符串后)附加到StringBuilder包含您希望添加的字符串的新字符串。结果实际上与前置相同,它只需要一个新的StringBuilder.

回答by Enigmativity

Your code seems like a lot of overkill to use StringBuilderwith those Forloops.

StringBuilder与这些For循环一起使用您的代码似乎有些矫枉过正。

Why not do this?

为什么不这样做?

Dim IntegerList() = {24, 12, 34, 42}

lstRandomList.Items.Add("The array before: ")
lstRandomList.Items.Add(String.Format("{{{0}}}", String.Join(", ", IntegerList)))

ReDim Preserve IntegerList(4)

lstRandomList.Items.Add("The array after: ")
lstRandomList.Items.Add(String.Format("{{{0}}}", String.Join(", ", IntegerList)))

Job done. Much simpler code.

任务完成。更简单的代码。