首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ParamArray中的几个数组

ParamArray中的几个数组
EN

Stack Overflow用户
提问于 2015-02-20 22:48:46
回答 2查看 146关注 0票数 0

我需要一个函数,它可以搜索一个字符串中不同数组中的一些字符串。

比方说,我有一个单词" building“和两个列表(=两个数组):1.房子,车库,塔,城堡,建筑物2.桌子,床,花,图片

因此,在这种情况下,列表1包含有关的单词,因此应该做出响应。

到目前为止我的代码(一维数组):

代码语言:javascript
复制
Function cbsMatchKeywords(strKeyword As String, ParamArray strList() As Variant) As String

    Dim i As Long

    For i = LBound(strList,1) + 1 To UBound(strList,1)
        If InStr(strKeyword, strList(i,1)) > 0 Then
            cbsMatchKeywords = cbsMatchKeywords & strList(i,1)
        End If
    Next i

End Function

有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2015-02-20 22:57:05

这对你来说很有用

代码语言:javascript
复制
Function cbsMatchKeywords(strKeyword As String, ParamArray strList() As Variant) As String

    Dim i As Long, j As Long
    For j = LBound(strList, 2) To UBound(strList, 2)
        For i = LBound(strList, 1) + 1 To UBound(strList, 1)
            If InStr(strKeyword, strList(i, j)) > 0 Then
                cbsMatchKeywords = cbsMatchKeywords & strList(i, j)
            End If
        Next i
    Next j

End Function
票数 0
EN

Stack Overflow用户

发布于 2015-02-21 04:55:20

代码语言:javascript
复制
Option Explicit

Public Sub Main()
    Dim arr1 As Variant
    arr1 = Array("house", "garage", "tower", "castle", "building")

    Dim arr2 As Variant
    arr2 = Array("table", "bed", "flowers", "picture")

    Const keyword As String = "building"

    Dim result As String
    result = cbsMatchKeywords(keyword, arr1, arr2)

    Debug.Print "Result is : '" & result & "'"
    ' Prints:
    ' Result is : 'building'
End Sub

Function cbsMatchKeywords( _
    strKeyword As String, _
    ParamArray strList() As Variant) As String

    Dim i As Integer
    Dim j As Integer
    Dim arr As Variant

    For i = LBound(strList) To UBound(strList)

        arr = strList(i)
        If Not IsArray(arr) Then _
            GoTo continue

        For j = LBound(arr) To UBound(arr)
            If InStr(strKeyword, arr(j)) > 0 Then
                cbsMatchKeywords = cbsMatchKeywords & arr(j)
            End If
        Next j

continue:
    Next i

End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28631738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档