在 vb.net 中将 Arraylist 转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2357028/
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
Convert Arraylist into string in vb.net
提问by acadia
How do I convert an arraylist into a string of comma delimated values in vb.net
如何在 vb.net 中将数组列表转换为逗号分隔值的字符串
I have an arraylist with ID values
我有一个带有 ID 值的数组列表
arr(0)=1
arr(1)=2
arr(2)=3
I want to convert it into a string
我想把它转换成字符串
Dim str as string=""
str="1,2,3"
回答by Nick Allen
str = string.Join(",", arr.ToArray());
If you need to convert the List to string[] before the string.Join you can do
如果你需要在 string.Join 之前将 List 转换为 string[] 你可以
Array.ConvertAll<int, string>(str.ToArray(), new Converter<int, string>(Convert.ToString));
So...
所以...
str = string.Join(",", Array.ConvertAll<int, string>(str.ToArray(), new Converter<int, string>(Convert.ToString)));
回答by Muhammad Saqib
You can simply achieve it from GetTypeand JoinFunctions.
你可以简单地从GetType和Join函数中实现它。
Dim S = YourArrayList.ToArray(Type.GetType("System.String"))
MessageBox.Show(String.Join(",", S))
Another way is to use FOR EACHStatement to read and store each item of array one by one in a delimited string. (But not recommended)
另一种方法是使用FOR EACHStatement 将数组中的每一项逐项读取并存储在一个分隔的字符串中。(但不推荐)
Dim S as string = ""
For Each item As String In YourArrayList
S &= item & ", "
Next
MessageBox.Show(S)
回答by Redips77
回答by Matt Dearing
Use String.Join with a comma delimeter (http://msdn.microsoft.com/en-us/library/57a79xd0.aspx)
使用带有逗号分隔符的 String.Join ( http://msdn.microsoft.com/en-us/library/57a79xd0.aspx)

