Convert string array to string in VB

Asked

Viewed 488 times

3

I need to use SPLIT in multiple data, but I need to convert the string to string array for this.

I’m putting String() to receive the string array variable, but VS keeps telling me that one dimension array cannot be converted to string.

Can someone help me?

2 answers

2

Catharina, it is not very clear the situation you have, so it is difficult to give a categorical answer.

To concatenate items from an array into a single String, without separators, you can use the method String.Concat()

To concatenate items of an array into a single string, including a separation string, the method, already indicated by others, is Join()

To complete a preformed string with array items, the preferred method is String.Format()

Now, did you mention that you need to use the SPLIT and I didn’t understand, because Split() does exactly the opposite: divides a String into several fragments and returns an Array.

Below are some examples. If you put your code here, it makes it easier to give an answer.

Sub ExemplosStringArray()
    Dim arrayOfString As String() = {"aaaa", "bbbb", "cccc"}
    Dim concatStrings = String.Concat(arrayOfString)
    MsgBox(concatStrings)
    Dim joinedStrings = Join(arrayOfString, ";")
    MsgBox(joinedStrings)
    Dim formattedString = String.Format("Temos {0}, {1} e finalmente {2}", arrayOfString)
    MsgBox(formattedString)
    Dim multipartString = "dddd-eeee+ffff"  
    Dim arrayFromString = multipartString.Split("-"c, "+"c)
    Dim newStringFromArray = Join(arrayFromString, vbCrLf)
    MsgBox(newStringFromArray)
End Sub

2

You can use the method String.Join that waits for a separator and parameters to concatenate and return a string (a collection in general). Example:

Dim result as String = String.Join("", array)
  • It concatenates without separators?

  • Yes, since you inform that the separator is an empty string, String.Empty.

  • Right... He returns me intact? No modifications?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.