簡體   English   中英

從數組visual basic中刪除元素6

[英]Removing element from array visual basic 6

我有一個由多個字符串組成的數組,我想刪除其中一個字符串。 如果我的數組有一個相同的元素,如["a", "b", "b", "c"] ,我想刪除其中一個b,我的方法將刪除兩者。 到目前為止,這是我的代碼:

Private Function Search_Array(Character As String) As Boolean

    For Loop_Index = 1 To UBound(Characters_Array)
        If Characters_Array(Loop_Index) = Character Then
            Search_Array = True
            Characters_Array(Loop_Index) = ""
        End If
    Next

End Function

有沒有人有任何建議我如何只從陣列中刪除其中一個b。 非常感謝。

在將找到的第一個字符設置為空字符串后,您只需要退出循環。

Private Function Search_Array(Character As String) As Boolean

    For Loop_Index = 1 To UBound(Characters_Array)
        If Characters_Array(Loop_Index) = Character Then
            Search_Array = True
            Characters_Array(Loop_Index) = ""
            Exit For
        End If
    Next

End Function

對我有用的最好的方法是你創建新的函數,在這個函數中你將值放在新的數組中,並將舊的aray重新設置為空,然后你將看看哪個項目用if添加回舊數組。 請參閱下面的代碼。

Private Function arrClean_ARR(arrOld As Variant) As variant
On Error GoTo ErrLn
    Const c_strProcName = "countARR"
Dim i As Long
Dim arrayValues As Variant
Dim count As Long

' put values in new array
arrayValues = arrOld 

count = 0
For i = LBound(arrayValues) To UBound(arrayValues)
  'if value of the new array is "" then don't add in old array. Here you can   put your value (whatever) and it will not be in array. In that way you actualy remove value from array
  If arrayValues(i) <> "" Then
    ReDim Preserve arrOld (count)
    arrOld (i) = arrayValues(i)
    count = count + 1
  End If
Next i
arrClean_ARR = arrOld 
Exit Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM