0

我正在创建一个数组,如下所示

  Dim strFriends(0 to 6) As String

    strFriends(0) = "Bianca"
    strFriends(1) = "Jeana"
    strFriends(2) = "Sam"
    strFriends(3) = "Jenna"
    strFriends(4) = "Erin"
    strFriends(5) = "Carolyn"
    strFriends(6) = "Kate" 

    Dim myFriends As String

    myFriends = Join(strFriends, ", ")

    MsgBox myFriends

这将产生以下字符串:“Bianca, Jeana, Sam, Jenna, Erin, Carolyn, Kate”

但我需要检索数组中的特定项目并将它们显示为列表,如下所示:

例如:如果我想从strFriends, Kate , SamBianca中选择

它应该像这样列出

Kate
Sam
Bianca

如何执行任务。我对VB真的很陌生,所以我对这个简单的任务感到困惑。任何人都可以帮忙。

谢谢

4

2 回答 2

1
    Dim strFriends(0 To 6) As String

    strFriends(0) = "Bianca"
    strFriends(1) = "Jeana"
    strFriends(2) = "Sam"
    strFriends(3) = "Jenna"
    strFriends(4) = "Erin"
    strFriends(5) = "Carolyn"
    strFriends(6) = "Kate"

    Dim objOutput As Text.StringBuilder = New Text.StringBuilder()

    For Each strFriend As String In strFriends
        Select Case strFriend
            Case "Kate", "Sam", "Bianca"
                objOutput.AppendLine(strFriend)
        End Select
    Next

    MessageBox.Show(objOutput.ToString())

或者

    For Each strFriend As String In strFriends
        If MyLogicToDetermineSelected(strFriend) Then
            objOutput.AppendLine(strFriend)
        End If
    Next
于 2012-07-31T10:46:31.883 回答
0

当您已经知道想要哪些项目时,尚不清楚为什么需要从原始数组中选择项目,所以我不会回答这部分问题。至于在自己的行上显示每个项目,您可以这样做String.Join

Dim chosenFriends As String() = {"Kate", "Sam", "Bianca"}
Dim output As String = String.Join(Environment.NewLine, chosenFriends)
于 2012-07-31T13:19:12.913 回答