如何在VB.NET中将字符串转换为字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6868885/
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 convert string to string array in VB.NET?
提问by Yugal Jindle
Well, I have a function that takes a string array as input...
好吧,我有一个将字符串数组作为输入的函数...
I have a string to process from that function...
我有一个字符串要从该函数处理...
So,
所以,
Dim str As String = "this is a string"
func(// How to pass str ?)
Public Function func(ByVal arr() As String)
// Processes the array here
End Function
I have also tried:
我也试过:
func(str.ToArray) // Gives error since it converts str to char array instead of String array.
How can I fix this?
我怎样才能解决这个问题?
回答by Meta-Knight
With VB10, you can simply do this:
使用 VB10,您可以简单地执行以下操作:
func({str})
With an older version you'll have to do:
使用旧版本,您必须执行以下操作:
func(New String() {str})
回答by Jimmy
Just instantiate a new array including only your string
只需实例化一个仅包含您的字符串的新数组
Sub Main()
Dim s As String = "hello world"
Print(New String() {s})
End Sub
Sub Print(strings() As String)
For Each s In strings
Console.WriteLine(s)
Next
End Sub
回答by Naga Harish M
Use the String.Splitmethod to split by "blank space". More details here:
使用String.Split按“空格”分割的方法。更多细节在这里:
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
http://msdn.microsoft.com/en-us/library/system.string.split.aspx
If character by character then write your own function to do that.
如果一个字符一个字符然后写你自己的函数来做到这一点。
Try this code:
试试这个代码:
Dim input As String = "characters"
Dim substrings() As String = Regex.Split(input, "")
Console.Write("{")
For ctr As Integer = 0 to substrings.Length - 1
Console.Write("'{0}'", substrings(ctr))
If ctr < substrings.Length - 1 Then Console.Write(", ")
Next
Console.WriteLine("}")
' The example produces the following output:
' {'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}
Using Regex, http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx#Y2166.
使用正则表达式,http://msdn.microsoft.com/en-us/library/8yttk7sy.aspx#Y2166。
回答by Khasm
Can you just put that one string in to an array? My VB is rusty, but try this:
你能把那个字符串放到一个数组中吗?我的 VB 生锈了,但试试这个:
Dim arr(0) As String
arr(0) = str
func(arr)
回答by Jon Skeet
I'm not a VB expert, but this looks like the cleanest way to me:
我不是 VB 专家,但这对我来说似乎是最干净的方法:
func(New String() { str })
However, if that's not clean enough for you, you could use extension methods eitherspecific to string:
然而,如果这还不够干净适合你,你可以使用扩展方法或者特定的字符串:
func(str.ToStringArray)
orin a generic way:
或以通用方式:
func(str.ToSingleElementArray)
Here's the latter as an extension method:
这是后者作为扩展方法:
<Extension> _
Public Shared Function ToSingleElementArray(Of T)(ByVal item As T) As T()
Return New T() { item }
End Function
回答by scwagner
You mean like String.ToCharArray? Also, you can access the chars contained in the string directly.
你的意思是像String.ToCharArray?此外,您可以直接访问字符串中包含的字符。

