bjaeger
bjaeger

Reputation: 85

Convert integer array to string array

whats the easiest way to convert an array of integers into string form? I'm trying to copy the whole array of integers into strings.

{1, 2, 3}

to

{"1", "2", "3"}

Upvotes: 3

Views: 11425

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

The easiest method would be to use the Select extension method which is provided by LINQ:

Dim intArray() As Integer = {1, 2, 3}
Dim strArray() As String = intArray.Select(Function(x) x.ToString()).ToArray()

If you don't want to, or cannot use LINQ, you can use the Array.ConvertAll method, which is almost as easy:

 Dim strArray() As String = Array.ConvertAll(Of Integer, String)(intArray, Function(x) x.ToString())

EDIT

Based on your comments, below, it looks like you need to convert from an ArrayList of integers to an ArrayList of strings. In that case, you could do it like this:

Dim intArray As New ArrayList({1, 2, 3})
Dim strArray As New ArrayList(intArray.ToArray().Select(Function(x) x.ToString()).ToArray())

Although, at that point, it's starting to get a bit messier. It's probably easier to just do a standard loop, like this:

Dim myArray As New ArrayList({1, 2, 3})
For i As Integer = myArray.Count - 1 To 0 Step -1
    myArray(i) = myArray(i).ToString()
Next

For what it's worth, though, unless you are still on a really old version of the .NET Framework, you really ought to be using the List(Of T) class rather than the ArrayList class, in most cases.

Upvotes: 9

Related Questions