vb.net 如何让数组一次显示所有值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8735210/
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
How to get an array to display all its values at once
提问by Mark Kramer
Here is some sample code:
下面是一些示例代码:
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
How can I display those four values next to each other.
我怎样才能显示这四个值彼此相邻。
More specifically, given those values how can I make txtValue.Text = 5471
更具体地说,鉴于这些值,我怎样才能使 txtValue.Text = 5471
Edit:
编辑:
The idea I had would be to use some sort of function to append each one to the end using a loop like this:
我的想法是使用某种函数使用这样的循环将每个函数附加到末尾:
Dim finalValue
For i As Integer = 3 To 0 Step -1
arrValue(i).appendTo.finalValue
Next
Obviously that code wouldn't work though the premise is sound I don't know the syntax for appending things and I'm sure I wouldn't be able to append an Integer anyway, I would need to convert each individual value to a string first.
显然,尽管前提是合理的,但该代码不起作用我不知道附加内容的语法,而且我确定无论如何我都无法附加一个整数,我需要将每个单独的值转换为字符串第一的。
回答by Chris Dunaway
Another method is to use String.Join:
另一种方法是使用 String.Join:
Sub Main
Dim arrValue(3) as Integer
arrValue(0) = 5
arrValue(1) = 4
arrValue(2) = 7
arrValue(3) = 1
Dim result As String = String.Join("", arrValue)
Console.WriteLine(result)
End Sub
回答by Guffa
Convert the integers to strings, and concatenate them:
将整数转换为字符串,并将它们连接起来:
Dim result as String = ""
For Each value as Integer in arrValue
result += value.ToString()
Next
Note: using +=
to concatenate strings performs badly if you have many strings. Then you should use a StringBuilder
instead:
注意:+=
如果你有很多字符串,使用连接字符串的效果会很差。那么你应该使用 aStringBuilder
代替:
Dim builder as New StringBuilder()
For Each value as Integer in arrValue
builder.Append(value)
Next
Dim result as String = builder.ToString()
回答by Justin Self
If I understand your question correctly, you can use StringBuilder to append the values together.
如果我正确理解您的问题,您可以使用 StringBuilder 将值附加在一起。
Dim finalValue as StringBuilder
finalValue = new StringBuilder()
For i As Integer = 3 To 0 Step -1
finalValue.Append(arrValue(i))
Next
Then just return the finalValue.ToString()
然后只需返回 finalValue.ToString()
回答by ???
for i = lbound(arrValue) to ubound(arrValue)
ss=ss & arrValue(i)
next i
end for
debug.print ss
回答by kahila kalombo
Dim value as string = ""
For A As Integer = 1 To Begin.nOfMarks
value += "Mark " & A & ": " & (Begin.Marks(A)) & vbCrLf
Next A