fastest, easiest way to remove item from a string array?

C

Craig Buchanan

what is the fastest way to remove a value from a string array? something
like:

dim x as string() = {"A","B","C","D"}

'remove C
x.Clear(x, x.IndexOf(x, "C"), 1)


Questions:

1). if i'm calling x.clear, do i need to keep specifying x in the
..indexof method? can i use me.?
2). does Clear resize the array?

thanks,

Craig
 
A

Anand Balasubramanian

Hi,
I looked at your code. Basically when you say

dim x as string() = {"A","B","C","D"}

It is an array of strings. Basically "A" is a string by itself. You could
also say something like

dim x as string() = {"abcd","hello"}

So now when you say x.clear(...), it will basically just make the index
that you specifly null. It will not resize the array.


But if you are just looking to create a string and manipulate its
characters then there are two ways to do it.

1) Use the System.String class and create a string as follows

Dim x as new String("Hello World")

then you can call x.remove(0,1), this will basically return a string with
one character removed. The origninal x will remain unchanged.The drawback
to using the String class is that it will generate a new string for every
string operation. So the other way to do is as follows

2) You can use the System.Text.Stringbuidler class. This class will do all
the operations on the source string. It is more like direct mainpulation of
the string stored in memory For example
Dim str As New String("hello world!")
MsgBox(str.Remove(0, 1))
MsgBox(str)

Dim MyStringBuilder As New StringBuilder("Hello World!")
MyStringBuilder.Remove(5, 7)
MsgBox(MyStringBuilder.ToString)

Str will still have the same value, but mystringbuilder will have only the
resultant value of the remove operation.

The following link has a detailed description

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconusingstringbuilderclass.asp



Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top