How can you remove a specific string from an array (totally), and redim it so that the blank doesnt remain?
IE> In an array (3)
Hello
Test
Help
and i want to remove "Test".. to make the new array(2) look like this:
Hello
Help
anyone know?
Printable View
How can you remove a specific string from an array (totally), and redim it so that the blank doesnt remain?
IE> In an array (3)
Hello
Test
Help
and i want to remove "Test".. to make the new array(2) look like this:
Hello
Help
anyone know?
While it can be done with an array, it's easier to use a collection.
VB Code:
Dim MyCollection As New Collection Dim intIndex As Integer ' Create the test data MyCollection.Add "Hello", "Hello" ' 1st is Item, 2nd is key. Keys must be unique MyCollection.Add "Test", "Test" MyCollection.Add "Help", "Help" ' Remove one of them MyCollection.Remove "Test" ' Show the results For intIndex = 1 To MyCollection.Count Debug.Print MyCollection(intIndex) Next
arent collections a lot slower than arrays though?
They are also bigger do, but you won't notice either disadvantage unless you have thousands of strings. They will be faster than the array for doing what you want to do.
how about:
if a() contains your array of strings
just a thoughtCode:
for i = lbound(a) to ubound(a)
if a(i) <> YourSearchString then
b(j) = a(i)
j=j+1
next i
redim a(j)
a=b
kevin
ps I haven't tested it
the only drawback to the collection is that unique key..
i wont be adding all unique items to the array tho
how can i get around that?
You don't need to inclue a key. It is an optional thing. Just don't enter anything there.
In that case
VB Code:
Dim intIndex As Integer Dim MyArray() As String Dim intTwo As Integer Dim bDeleted As Boolean ReDim MyArray(4) MyArray(0) = "Hello" MyArray(1) = "Test" MyArray(2) = "Help" MyArray(3) = "one" MyArray(4) = "Test" bDeleted = True Do Until bDeleted = False bDeleted = False For intIndex = 0 To UBound(MyArray) If MyArray(intIndex) = "Test" Then For intTwo = intIndex To UBound(MyArray) - 1 MyArray(intTwo) = MyArray(intTwo + 1) bDeleted = True Next ReDim Preserve MyArray(UBound(MyArray) - 1) End If If bDeleted Then Exit For End If Next Loop
But then you can't remove an item without looping through the collection, and the ability to "grab" the item in this case is the advantage of the collection.Quote:
Originally posted by BuggyProgrammer
You don't need to inclue a key. It is an optional thing. Just don't enter anything there.