VB.NET 空字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44713/
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
VB.NET Empty String Array
提问by YonahW
How can I create an empty one-dimensional string array?
如何创建一个空的一维字符串数组?
采纳答案by G8RDA
Dim strEmpty(-1) As String
Dim strEmpty(-1) 作为字符串
回答by Mark Brackett
VB is 0-indexed in array declarations, so seomthing like Dim myArray(10) as Stringgives you 11elements. It's a common mistake when translating from C languages.
VB在数组声明中是0 索引的,所以类似的东西Dim myArray(10) as String给了你11 个元素。从 C 语言翻译时,这是一个常见的错误。
So, for an empty array, either of the following would work:
因此,对于空数组,以下任一方法都可以:
Dim str(-1) as String ' -1 + 1 = 0, so this has 0 elements
Dim str() as String = New String() { } ' implicit size, initialized to empty
回答by Joel Coehoorn
The array you created by Dim s(0) As StringIS NOT EMPTY
您创建的数组Dim s(0) As String不是空的
In VB.Net, the subscript you use in the array is index of the last element. VB.Net by default starts indexing at 0, so you have an array that already has one element.
在 VB.Net 中,您在数组中使用的下标是最后一个元素的索引。VB.Net 默认从 0 开始索引,所以你有一个已经有一个元素的数组。
You should instead try using System.Collections.Specialized.StringCollectionor (even better) System.Collections.Generic.List(Of String). They amount to pretty much the same thing as an array of string, except they're loads better for adding and removing items. And let's be honest: you'll rarely create an emptystring array without wanting to add at leastone element to it.
您应该尝试使用System.Collections.Specialized.StringCollectionor (甚至更好)System.Collections.Generic.List(Of String)。它们与字符串数组几乎相同,只是它们更适合添加和删除项目。老实说:您很少会创建一个空字符串数组而不希望向其中添加至少一个元素。
If you really want an empty string array, declare it like this:
如果你真的想要一个空字符串数组,像这样声明它:
Dim s As String()
or
或者
Dim t() As String
回答by JustinMichel
You don't have to include String twice, and you don't have to use New.
Either of the following will work...
您不必两次包含 String,也不必使用 New。
以下任何一种都将起作用...
Dim strings() as String = {}
Dim strings as String() = {}
回答by Chris Zwiryk
Something like:
就像是:
Dim myArray(9) as String
Would give you an array of 10 String references (each pointing to Nothing).
会给你一个包含 10 个字符串引用的数组(每个都指向 Nothing)。
If you're not sure of the size at declaration time, you can declare a String array like this:
如果在声明时不确定大小,可以像这样声明一个 String 数组:
Dim myArray() as String
And then you can point it at a properly-sized array of Strings later:
然后你可以稍后将它指向一个适当大小的字符串数组:
ReDim myArray(9) as String
ZombieSheep is right about using a List if you don't know the total size and you need to dynamically populate it. In VB.NET that would be:
如果您不知道总大小并且需要动态填充它,ZombieSheep 使用 List 是正确的。在 VB.NET 中,这将是:
Dim myList as New List(Of String)
myList.Add("foo")
myList.Add("bar")
And then to get an array from that List:
然后从该列表中获取一个数组:
myList.ToArray()
@Mark
@标记
Thanks for the correction.
谢谢指正。
回答by Michael Johnson
Another way of doing this:
这样做的另一种方法:
Dim strings() As String = {}
Testing that it is an empty string array:
测试它是一个空字符串数组:
MessageBox.Show("count: " + strings.Count.ToString)
Will show a message box saying "count: 0".
将显示一个消息框,上面写着“计数:0”。
回答by Scott Mitchell
A little verbose, but self documenting...
有点冗长,但自我记录......
Dim strEmpty() As String = Enumerable.Empty(Of String).ToArray
回答by ZombieSheep
Not sure why you'd want to, but the C# way would be
不知道你为什么想要,但 C# 的方式是
string[] newArray = new string[0];
I'm guessing that VB won't be too dissimilar to this.
我猜 VB 不会与此有太大不同。
If you're building an empty array so you can populate it with values later, you really should consider using
如果您正在构建一个空数组以便稍后用值填充它,您真的应该考虑使用
List<string>
and converting it to an array (if you really need it as an array) with
并将其转换为数组(如果您确实需要将其作为数组)
newListOfString.ToArray();
回答by Lisa
try this Dim Arraystr() as String ={}
试试这个 Dim Arraystr() as String ={}
回答by Dennis Spade
Dim array As String() = Array.Empty(Of String)
Dim array As String() = Array.Empty(Of String)

