5

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?

2
  • How do you want it turned into an array? Delimited by spaces? Commented Jul 29, 2011 at 5:13
  • No.. nothing like that.. Simply convert "mystring" to an array with array with single element "mystring". Commented Jul 29, 2011 at 5:16

6 Answers 6

8

With VB10, you can simply do this:

func({str})

With an older version you'll have to do:

func(New String() {str})
Sign up to request clarification or add additional context in comments.

Comments

5

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

4 Comments

That works.. but can we do it in a neater way.. ? Something more intuitive like str.ToStringArray or something ?
You're trying to create an object of a new type (in this case String()) instead of the one you have. Since there isn't a built-in way to do so, is going through hoops a "neater" way? You could always write an extension method to do it.
@Yugal Meta-Knight's answer is the neatest thing I can imagine. Just add two braces Print({s})
Certainly.. Thumbs up.. to Meta-Knight !
3

Use the String.Split method to split by "blank space". More details here:

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.

2 Comments

No.. i don't want my string to split !!
So, I misunderstand your question... :(
2

Can you just put that one string in to an array? My VB is rusty, but try this:

Dim arr(0) As String
arr(0) = str

func(arr)

Comments

1

I'm not a VB expert, but this looks like the cleanest way to me:

func(New String() { str })

However, if that's not clean enough for you, you could use extension methods either specific to string:

func(str.ToStringArray)

or in 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

1 Comment

The cleanest way is func( { str } ) as in Meta-Knight's answer
-1

You mean like String.ToCharArray? Also, you can access the chars contained in the string directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.