string 我如何添加到 vb.net 中的简单数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11421716/
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-09 01:32:40  来源:igfitidea点击:

How do i add to a simple array in vb.net

vb.netarraysstringadd

提问by masteroleary

If I have

如果我有

Dim a As String() = ("One,Two").Split(",")

Dim a As String() = ("One,Two").Split(",")

How can I add to that string ?

如何添加到该字符串?

回答by Holger Brandt

The easiest way is to convert it to a List and then add.

最简单的方法是将其转换为List,然后添加。

Dim a As List(Of String) = ("One,Two").Split(",").ToList
a.Add("Three")

or if you really want to keep an array.

或者如果你真的想保留一个数组。

    Dim a As String() = ("One,Two").Split(",")
    Dim b as List(Of String) = a.ToList
    b.Add("Three")
    a=b.ToArray

And here is something really outside the box:

这里有一些真正开箱即用的东西:

a = (String.Join(",", a) & ",Three").Split(",")

回答by Tony Dallimore

For a different approach, try:

对于不同的方法,请尝试:

Dim a As String() = ("One,Two").Split(CChar(","))
Debug.Print(CStr(UBound(a)))
ReDim Preserve a(9)
Debug.Print(CStr(UBound(a)))

The output to the immediate window is:

立即窗口的输出是:

1
9

Note: I have had to slightly change your original line because I always use Option Strict Onwhich does not permit implicit conversions.

注意:我不得不稍微更改您的原始行,因为我总是使用Option Strict On不允许隐式转换的内容。