Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integer array to string array

Tags:

vb.net

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"}

like image 746
bjaeger Avatar asked Apr 22 '26 06:04

bjaeger


1 Answers

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.

like image 91
Steven Doggart Avatar answered Apr 26 '26 19:04

Steven Doggart