简体   繁体   English

从数组visual basic中删除元素6

[英]Removing element from array visual basic 6

I've an array which consists of multiple strings, I want to remove one of the strings. 我有一个由多个字符串组成的数组,我想删除其中一个字符串。 If my array has one of the same elements so ["a", "b", "b", "c"] and I want to remove one of the b's, my method will remove both. 如果我的数组有一个相同的元素,如["a", "b", "b", "c"] ,我想删除其中一个b,我的方法将删除两者。 Here's my code so far: 到目前为止,这是我的代码:

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

Does anyone have any suggestion as to how I can remove only one of the b's from the array. 有没有人有任何建议我如何只从阵列中删除其中一个b。 Thanks a lot. 非常感谢。

You just need to exit the loop after setting the first character found to an empty string. 在将找到的第一个字符设置为空字符串后,您只需要退出循环。

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

The best way which works for me is that you create new function and in that function you put values in new array, and redim old aray to empty, then you will look which item to add back to old array with if . 对我有用的最好的方法是你创建新的函数,在这个函数中你将值放在新的数组中,并将旧的aray重新设置为空,然后你将看看哪个项目用if添加回旧数组。 See the code below. 请参阅下面的代码。

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