0

If I have

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

How can I add to that string ?

masteroleary
  • 1,014
  • 2
  • 16
  • 33
  • possible duplicate of [Fastest way to add an Item to an Array](http://stackoverflow.com/questions/18097756/fastest-way-to-add-an-item-to-an-array) – Carl Onager Feb 05 '14 at 14:41

2 Answers2

4

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(",")
Holger Brandt
  • 4,324
  • 1
  • 20
  • 35
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.

Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61