How to get sub-array from larger array in VB.NET?

后端 未结 3 1305
梦毁少年i
梦毁少年i 2021-01-13 23:06

I have:

Dim arr() As String = {\"one\",\"two\",\"three\"}

I want a new array, sub, containing {\"one\", \"three\"} only. What

3条回答
  •  猫巷女王i
    2021-01-13 23:52

    You can use the Array.Copy method. I don't know how you are picking elements from your array. Are you randomly picking or any other way? But I think you need to create a second array and use the Copy method.

    You can use the Copy method in a variety of ways. The link I gave above is to pick an element from a specified index of the first array and copy to a specified index of the second array.

    Here is an example in C#:

    string[] firstArray = {"dog","cat","fish","monkey"};
    string[] secondArray = firstArray;
    Array.Copy(firstArray,3,secondArray,0,1);
    Console.WriteLine(secondArray[0].ToString());
    

    A VB.NET example is here:

    Arrays: Copy vs. Clone

    In your case, you can put the Array.Copy in a loop and keep changing the source and destination index.

提交回复
热议问题