vb.net 将数组元素转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18334741/
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 convert array element into string
提问by ar.dll
Using the code below to loop through my array which was created by reading a text file with 10 or so lines in it, so each thing in the array is one of those lines from the text file
使用下面的代码循环遍历我的数组,该数组是通过读取包含 10 行左右的文本文件创建的,因此数组中的每一行都是文本文件中的那些行之一
Dim myarray As Array
myarray = Split(stringfromtextfile, vbCrLf)
For each element in myArray
MgBox(element)
Dim splititem As String = Split(element, "\")
The message box shows the line as I'm expecting but when I try to split it up I just get the error:
消息框显示了我期望的行,但是当我尝试将其拆分时,我只收到错误消息:
Error 1 Value of type '1-dimensional array of String' cannot be converted to 'String'.
错误 1 类型“一维字符串数组”的值无法转换为“字符串”。
How do I get the value which was shown in the message box converted to a string to I can then do a split on it?
如何将消息框中显示的值转换为字符串,然后我可以对其进行拆分?
Cheers!
干杯!
回答by Cody Gray
This line of code is invalid:
这行代码无效:
Dim splititem As String = Split(element, "\")
The Splitfunctionreturns an array of strings, but you're trying to assign the result to a variable that represents only a single string.
该Split函数返回一个字符串数组,但您试图将结果分配给一个仅表示单个字符串的变量。
That's what the error message is telling you: the "value of type '1-dimensional array of String'" is what is being returned from the Splitfunction, and that "cannot be converted to 'String'" in order to store it in the splititemvariable.
这就是错误消息告诉您的内容:“类型为‘一维字符串’的值”是Split函数返回的值,并且“无法转换为‘字符串’”以便将其存储在splititem多变的。
Change it to look like this (note the parentheses, which indicate an arrayof String):
将其更改为如下所示(注意括号,表示字符串数组):
Dim splititem As String() = Split(element, "\")
And do strongly consider using the .NET Framework methods for string manipulation, rather than the old VB 6 methods. They are provided primarily for compatibility purposes with older code, not intended for use in new code.
并且强烈考虑使用 .NET Framework 方法进行字符串操作,而不是旧的 VB 6 方法。提供它们主要是为了与旧代码兼容,而不是为了在新代码中使用。
If you actually arewriting VB 6 code (which is what this looks like), rather than VB.NET code, you cannot assign variables at the point of their declaration. You'll need to split these up into separate statements:
如果您实际上正在编写 VB 6 代码(这看起来像这样),而不是 VB.NET 代码,则不能在变量声明点分配变量。您需要将这些拆分为单独的语句:
Dim splititem As String()
splititem = Split(element, "\")
回答by Tim Schmelter
I would use .NET methods instead of VB6 methods which are not strongly typed, hence less efficient and - more important - error-prone and less readable.
我将使用 .NET 方法而不是非强类型的 VB6 方法,因此效率较低且 - 更重要的是 - 容易出错且可读性较差。
For example with LINQ:
例如使用 LINQ:
Dim first10LineFields = From line In System.IO.File.ReadLines(path)
Let fields = line.Split("\"c)
Select fields
Take 10
Output:
输出:
For Each lineFields As String() In first10LineFields
Dim fieldsCommaSeparated = String.Join(",", lineFields)
MessageBox.Show(fieldsCommaSeparated)
Next

