Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to string array in VB.NET?

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?

like image 212
Yugal Jindle Avatar asked Dec 04 '22 07:12

Yugal Jindle


2 Answers

With VB10, you can simply do this:

func({str})

With an older version you'll have to do:

func(New String() {str})
like image 88
Meta-Knight Avatar answered Dec 29 '22 08:12

Meta-Knight


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
like image 44
Jimmy Avatar answered Dec 29 '22 09:12

Jimmy