By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,835 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

simple question about arrays

P: n/a
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
...
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies

P: n/a
"Boni" <oilia@nospam> schrieb:
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????


You'll have to create a new array of appropriate size and copy over the
items of the original array to the new array using 'Array.Copy' or
'Array.CopyTo'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2

P: n/a
It is ugly. Is there no better solution? If split(.) returns an array I
can't take other collection type. Is it really nothing more appropriate?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP10.phx.gbl...
"Boni" <oilia@nospam> schrieb:
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????


You'll have to create a new array of appropriate size and copy over the
items of the original array to the new array using 'Array.Copy' or
'Array.CopyTo'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
You can't 'delete' an element from the middle of an array. You need to use a
collection for that functionality, or run through your array, copying all non
blank elements into another array.

"Boni" wrote:
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
...

Nov 21 '05 #4

P: n/a
"Boni" <oilia@nospam> schrieb
It is ugly. Is there no better solution? If split(.) returns an
array I can't take other collection type. Is it really nothing more
appropriate?

In addition the other answers: You can't delete an item because you would
have to quarry out a piece of your RAM module. Sounds funny but that's how
it is. Just like you can't delete a line from a text file on your HD. You
have to overwrite it by the subsequent lines (or array elements). As the
length of an array is fixed, there would be one remaining ununsed item at
the end. To solve the problem, the whole array must be copied into a new
array - without the item to be deleted. The same is done by Redim Preserve
BTW.
Armin

Nov 21 '05 #5

P: n/a
"Boni" <oilia@nospam> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Dear all,
I am wondering, that I can't find how to delete an element from an array.


I'd suggest constucting an ArrayList from the String array :

Dim a As New ArrayList( SomeString.Split(","c) )

For iIdx as Integer = a.Count - 1 To 0 Step -1
If False Then
a.RemoveAt( iIdx )
End if
Next

HTH,
Phill W.
Nov 21 '05 #6

P: n/a
Boni wrote:
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
..


There is another option, you can use arraylist to do the removing for you.

dim Str() as string
dim Arr as new ArrayList(str.split(","))

for ii as integer = 0 to Arr.Count-1
if cstr(Arr(ii)).length = 0 then Arr.Remove(ii)
Next

Chris
Nov 21 '05 #7

P: n/a
I think you would find it quicker to dim another array the same size of array
"a" then copy only the non blank elements to the new array and count them.
Then redim preserve the new array to the count of non items, i.e.

dim b(a.upperbound) as string

dim count as integer =0
for i as integer= 0 to a.upperbound
if a(i) <>"" then
b(count)=a(i)
count = count+1
end if
next

redim preserve b(count-1)
--
Dennis in Houston
"Boni" wrote:
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
...

Nov 21 '05 #8

P: n/a

In addition the other answers: You can't delete an item because you would
have to quarry out a piece of your RAM module

Was it a joke? I did it and my PC is dead now! :)
Nov 21 '05 #9

P: n/a
Thank you all. ArrayList is what I was looking for:)

"Chris" <no@spam.com> schrieb im Newsbeitrag
news:eP**************@TK2MSFTNGP10.phx.gbl...
Boni wrote:
Dear all,
I am wondering, that I can't find how to delete an element from an array.
dim a as string()
a=SomeString.split(",")

for ...
if _string="" then a.delete????
..


There is another option, you can use arraylist to do the removing for you.

dim Str() as string
dim Arr as new ArrayList(str.split(","))

for ii as integer = 0 to Arr.Count-1
if cstr(Arr(ii)).length = 0 then Arr.Remove(ii)
Next

Chris

Nov 21 '05 #10

P: n/a
"Boni" <oilia@nospam> schrieb

In addition the other answers: You can't delete an item because
you would have to quarry out a piece of your RAM module

Was it a joke? I did it and my PC is dead now! :)

Sorry, forgot the smiley. ;-)

Armin
Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.