我想要做类似这样的事情:
Dim Foo as String = "a,b,c,d,e"
Dim Boo as List(of String) = Foo.Split(","c)
当然,Foo.Split
返回的是一个一维的 String
数组,而不是通用的 List
。是否有一种方法可以在不迭代数组将其转换为通用的 List
的情况下完成此操作?
如果您不想使用LINQ,可以这样做:
Dim foo As String = "a,b,c,d,e"
Dim boo As New List(Of String)(foo.Split(","c))
String foo = "a,b,c,d,e";
List<String> boo = new List<String>(foo.Split(","));
你真的需要一个List<T>吗?还是IList<T>就足够了?因为string[]已经实现了后者……这也是为什么在可行的情况下编程应该针对接口而非具体实现。尽管可能在这种情况下你确实不行。
Dim Foo as String = "a,b,c,d,e"
Dim Boo as List(of String)
Boo.AddRange(Foo.Split(","c))
为了进一步完善答案,我发现以下内容非常有帮助:
Return New List(Of String)(IO.File.ReadAllLines(sFileName))
Dim Foo as String = "a,b,c,d,e"
Dim Boo as List(of String)
Boo = Split(Foo, ",").ToList
Dim Foo as String = "a<blah>b<blah>c<blah>d<blah>e"
Dim Boo as List(of String)
Boo = Split(Foo, "<blah>").ToList
var values = labels.Split(" "[0]).ToList<string>();
Dim strings As List<string> = string_variable.Split().ToList<string>();
c
来将字符串转换为字符... 我一直使用new Char() { "," }
。这样看起来更加整洁。 - freefaller