带有字符串的 VB.NET 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16386079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:52:02  来源:igfitidea点击:

VB.NET Array with Strings

vb.net

提问by Pancit Palabok

how do i copy 2 to 3 elements from Array 1 to another Array

我如何将 2 到 3 个元素从数组 1 复制到另一个数组

Pets

宠物

dog 1
dog 2
dog 6
cat 1
cat 2

狗1
狗2
狗6
猫1
猫2

Dim pet as String = "dog"

i want to copy the array elements form Array 1 to Array 2 only the elements with "dog" LIKE using the string named pet

我想使用名为 pet 的字符串将数组元素从数组 1 复制到数组 2,只有带有“dog”LIKE 的元素

Dog

dog 1
dog 2
dog 6

狗 1
狗 2
狗 6

回答by Idle_Mind

Here's what @rynah's example essentially does under the hood:

以下是@rynah 示例的基本原理:

    Dim array1() As String = {"dog 1", "dog 2", "dog 6", "cat 1", "cat2"}

    Dim tmpList As New List(Of String)
    For Each value As String In array1
        If value.Contains("dog") Then
            tmpList.Add(value)
        End If
    Next

    Dim array2() As String = tmpList.ToArray
    For Each value As String In array2
        Debug.Print(value)
    Next