0

If I have

Dim a As String() = ("One,Two").Split(",")

How can I add to that string ?

1

2 Answers 2

5

The easiest way is to convert it to a List and then add.

Dim a As List(Of String) = ("One,Two").Split(",").ToList
a.Add("Three")

or if you really want to keep an array.

    Dim a As String() = ("One,Two").Split(",")
    Dim b as List(Of String) = a.ToList
    b.Add("Three")
    a=b.ToArray

And here is something really outside the box:

a = (String.Join(",", a) & ",Three").Split(",")
Sign up to request clarification or add additional context in comments.

Comments

1

For a different approach, try:

Dim a As String() = ("One,Two").Split(CChar(","))
Debug.Print(CStr(UBound(a)))
ReDim Preserve a(9)
Debug.Print(CStr(UBound(a)))

The output to the immediate window is:

1
9

Note: I have had to slightly change your original line because I always use Option Strict On which does not permit implicit conversions.

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.